Friday, June 29, 2007

Configuring a standalone machine to install Sun One Directory Server

Requirement:

  • An NTFS partition with 250 MB disk space.
  • Administrator’s Username and Password.
  • A Windows Server with correct OS version.
  • *** Properly configured TCP/IP (IP address, subnet mask and optional default gateway)
  • A network connection (to a hub or another computer via a crossover cable). If you don’t have a network connection, you can finish the Active Directory installation by installing Microsoft Loopback Adapter.
  • Ethernet/LAN (NIC) card needs to be installed.
  • An operational DNS Server which can be installed in the Domain controller itself.
  • The Windows 2000 or Windows Server 2003 CD media (or at least the ‘i386’ folder)

Pre-Install Configurations:

Most of the standalone machines don’t follow the domain forest structure instead they have WORKGROUP structure. Now, Sun One Directory Server installation requires a domain structure for proper installation/functioning. To migrate from the existing WORKGROUP environment to the Domain environment you need to follow some steps. These are mentioned as follows:

  • Right click on My Computer.
  • ** Go to Computer Name tab and observe the WORKGROUP field. This is to confirm that there is no existing Domain in the LAN/intranet/computer.
  • Open a command prompt and type the command dcpromo. This will open an “Active Directory Installation Wizard” as shown below:
  • Now, follow the steps giving necessary inputs to complete the installation.
*** Before proceeding we must make sure to set a static IP and DNS IP. Refer the screenshot below as a sample TCP/IP setting:
  • In the “Domain Controller Type” panel select the option “Domain controller for a new domain”.
  • In the “Create New Domain” panel select the option “Domain in a new forest” and put a valid Domain Name and Domain NetBIOS name in the subsequent panels.
  • If all the entries are valid then installation of the Active Directory will be completed successfully.
Installation of Sun One Directory Server:

After installing and configuring the Active Directory in your local machine we have both the DNS Server and DNS client running in this same machine. Now, if we check the ** Computer Name tab as mentioned above, you can observe that your computer is being migrated from the WOORKGROUP environment to Domain Forest structure. It looks some thing like this:
Now we are ready to install Sun One Directory Server on a standalone machine. Note that the “Full Computer Name” required for installation can be retrieved from the “Computer Name” tab.

Thursday, June 28, 2007

NoClassDefFoundError vs ClassNotFoundException

Let us go through the definitions of the above throwables from the java doc

java.lang.NoClassDefFoundError

Thrown if the Java Virtual Machine or a classloader tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

java.lang.ClassNotFoundException
Thrown when an application tries to load in a class through its string name using:



  • The forName method in class Class.

  • The findSystemClass method in class ClassLoader .

  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

Here the most important part is that the error java.lang.NoClassDefFoundError is thrown when a class is not found in the class path at runtime but the class was present at the time of compilation.

While the exception java.lang.ClassNotFoundException is thrown by the methods which tries to load the class at runtime but the class is not required to be present in the classpath at compile time.

Let me explain the above using an example. Let us consider following class:

MyPrint.java

package test;
public class MyPrint
{
public
void print(String str)
{
System.out.println(str);
}
}

The method "print" is called from a class Test
Test.java

import test.MyPrint;

class Test
{
public static void main(String[] args)
{
MyPrint mp =
new MyPrint();
mp.print("
Class is loaded");
}
}


Here to compile the Test.java file you have to make sure that MyPrint.class is there in the classpath.

Now if you remove the MyPrint.class from the classpath (after compilation) and try to run “java Test” you’ll receive following error:

Exception in thread "main" java.lang.NoClassDefFoundError: test/MyPrint
at Test.main(Test.java:7)


Let us modify the Test.java file a bit:

//No Import required
//import test.MyPrint;

import java.lang.reflect.Method;

class Test
{
public static void main(
String[] args) throws Exception
{
Class cls = Class.forName("test.MyPrint");
Method meth = cls.getMethod("
print", new Class[]{String.class});
meth.invoke(cls.newInstance(),
new Object[]{"Class is loaded"});
}
}
Please note the changes. Here we are using reflection to call the method. At the time of compilation you don’t need MyPrint.class to be present in the classpath. Also as because “Class.forName” method throws java.lang.ClassNotFoundException (which is not error), you need to catch the exception. Here we have just re-thrown the exception.


Run if you run “java Test”, following exception will be thrown:

Exception in thread "main" java.lang.ClassNotFoundException: test.MyPrint

Now people who have worked with application servers (or even web servers) must have seen java.lang.ClassNotFoundException often. There your servlets, bean class etc are specified in the descriptors (mainly XML files). So the application class loader reads the descriptor and tries to load the classes. In case the class loader fails to load the class, the exception java.lang.ClassNotFoundException is thrown. You’ll find same exception often while working on JDBC, Spring etc in which involves similar situation.

Resolution (in general):
java.lang.NoClassDefFoundError

  • Make sure the class is there in the classpath.

java.lang.ClassNotFoundException

  • Make sure the class is there in the classpath.
  • Make sure your configuration files (specially where you are specifying the classes) are proper.

In case you have multiple class loaders you need to keep in mind that classes loaded by child class loader can't be seen by parent class loaders.

Wednesday, June 20, 2007

Diving Deep into Java (I):

I thought, let’s highlight some concepts of Java which many of us may know or may not. But those are important and interesting. So let dive deep into Java.


1. Can you declare a class abstract & final at the same time?

The answer is no. Because when you declare a class abstract, it means you can’t create an instance of that class directly. First you need to create a subclass of that class and then you can create an instance of that subclass. So, abstract class is made to be sub classed. On the contrary, final class can’t be sub classed. So, I think now it is clear that a class can’t be abstract & final at the same time.

2. We all know that private members are invisible to any code outside the member’s own class. So, you are free to declare a method with the same name as in the super class. This will not fall in the overriding or overloading category.
You all know that in case of overloading you can’t change only the return type of an overloading method. You have to change the signature of the method.
But in case of the above scenario you don’t need to worry about the overloading rule.

If the point is still not clear let’s take an example –
class A
{
private void printName(String nm)
{
System.out.println(nm);
}
}

class B extends A
{
public int printName(String nm)
{
System.out.println(nm);
return nm.length();
}
}

So, if you look at the above example it seems that it is breaking the overloading rule. But as the “printName” method is private in class A, class B is free to declare a method with the same name. To class B, it is as if class A does not have any such method.

3. What type of access do you have to a protected member of a class inside its subclass which belongs to a different package?

Protected members are inherited inside it’s subclass out side the package. So inside that subclass you can access that member using “IS A” relationship. But you can’t access it using a “HAS A” relationship.

Let me make it clear with an example –

package pkg1;
public class A
{
protected int count = 10;
}

package pkg2;
import pkg1;
public class B extends A
{
A a1 = new A ();
System.out.println (count);
System.out.println (a1.count); // this line is wrong
}

The line number 7 inside the class B is incorrect. We can’t use the (.) operator on an instance of class A to access the member “count” from inside class B. The member “count” is only inherited. So, we can think as if “count” is a member of class B. That’s why line number 6 is correct.

4. A very small point – never declare a local variable, inside any method, with public-private-protected access modifiers. You can only use “final” with local variables.

5. What can’t be changed and can be changed when you declare an object reference variable as final?

You can’t assign a new instance to the reference variable. But you can change the values of the members of that object.

6. We know that declaring a variable with final keyword makes it impossible to reinitialize that variable. But do we know that this is only true when we declare it with an explicit value? Please note, I have said explicit value, not default value.

7. One interesting point to remember – variables declared inside an interface is implicitly “public static final”. You can also explicitly declare it as “public static final”. No problem in that.
But methods declared inside an interface can’t be final, private, static, protected, synchronized, native or strictfp.