ClassLoader problem

I have my main class and bunch of other classes (Say aClass and bClass) in a particular directory (/projects/classes/) and I use URLclassloader to load some other dynamic jar present in /usr/j2se/jre/lib/ext/ (say dynamicClass.jar). When I run my main class, its working fine till it loads the dynamic jar. Infact its even loading the dynamic jar.

The dynamic jar uses my normal classes (it has reference to aClass and bClass). The problem arises here.  I get "Exception in thread "main" java.lang.NoClassDefFoundError" . In my classpath, I have the path of my normal classes (aClass, bClass) and extensions. I dont know why the class in dynamicClass.jar file is unable to find my normal classes (aClass, bClass). They are in the classpath.

The portions of the code in the classes as described above:

Main class:

aClass whatever = new aClass();
int value = aClass.getValue();

String className = "dynamicClass";
Class classLoaded = Class.forName(className, true, ClassLoader.getSystemClassLoader());
 // Create an instance of the class

classInstance = classLoaded.newInstance(); //FAILS (Look at dynamicClass)


aClass aObject = new aClass; //FAILS
String aThing = aClass.getThing();

My class path:

java -classpath ../classes:/usr/j2se/jre/lib/ext/dynamicClass.jar$CLASSPATH -Djava.library.path=/projects/classes/mainClass

I tried including aClass and bClass in the classpath. It wouldn't work. If I jar up the aClass and bClass and place them under extension directory (i.e. :/usr/j2se/jre/lib/ext/), it works. Thats not what I want. I do not want to deliver my normal classes to extension directory.

I read an article on Classloaders at

But it did not help.

Thanks in advance.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
> classInstance = classLoaded.newInstance(); //FAILS (Look at dynamicClass)

what error?

Where is dynamicClass?
Mick BarryJava DeveloperCommented:
where are you using URLClassLoader?
You shouldn't need to include the /jre/lib/ext path in your classpath.  The class loader should look in there automatically.  (No doubt objects or CEHJ will correct this if it's wrong ;-)).

Are you sure that ../classes is correct?  This implies that you're executing the java command from another directory with the same parent as "classes".

The $CLASSPATH may also be a problem.  It depends what it expands to.

Finally, I don't think you need the -D option.

This leaves us with ... er ...

java -classpath ../classes YourClass

(If the ../classes is correct).
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

JoegalAuthor Commented:

I tried both URLClassLoader and ClassLoader. None of them makes difference. The error I encounter is "Exception in thread "main" java.lang.NoClassDefFoundError"

Let me re-phrase the problem precisely in steps:

1) I have three classes "mainClass", "aClass" and "bClass" in a directory /projects/classes

2) I have a class "dynamicClass" in extension directory /usr/j2se/jre/lib/ext/ as "dynamicClass.jar" file which uses objects "aClass" and "bClass" in its code

3) I am trying to load the "dynamicClass" using ClassLoader (or URLClassLoader) from my "mainClass"

4) My "mainClass" is able to find the dynamicClass successfully in the extension directory

5) But my "dynamicClass" is unable to find "aClass" and "bClass" (Read step 2 to understand how the dynamicClass uses "aClass" and "bClass"  -  I basically do aClass aObject = new aClass .....). I get an error "Exception in thread "main" java.lang.NoClassDefFoundError:aClass"

6) If I create 2 jars "aClass.jar" and "bClass.jar" and put them in /usr/j2se/jre/lib/ext/ directory (the same one as "dynamicClass.jar" directory), I dont see an problem.

I would like to know how I can fix the problem in step 5 without doing step 6. How do I setup the classpath so that the class found in extension directory can reference other normal classes? Do I need to use "Class.forName(className, true, ClassLoader.getSystemClassLoader());" in a different manner?


Mick BarryJava DeveloperCommented:
> The error I encounter is "Exception in thread "main" java.lang.NoClassDefFoundError"

You shouldn't get that error from newInstance, are you sure its not coming from Class.forName(). Can you post the full stack trace.

> I would like to know how I can fix the problem in step 5 without doing step 6

they need to be available to the classloader, whichever that may be. So for the system classloader to find them you'd need to add /projects/classes to the classpath.
For ULRClassLoader to find them you'd need to pass the the URL to that directory in the url list passed to its ctor.
JoegalAuthor Commented:

I am sure as the code when run is able to system out "Success" but its not printing "Success2" as the code is failing in  "classInstance = classLoaded.newInstance();" in my following code.  

Regarding the classpath, I already have "/projects/classes" in my classpath (java -classpath ../classes:/usr/j2se/jre/lib/ext/dynamicClass.jar$CLASSPATH -Djava.library.path=/projects/classes/mainClass). I am running my code from /projects/bin through a run shell script (that why I have ../classes).

classLoaded = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
//classLoaded = Class.forName(className, true, ClassLoader.getSystemClassLoader(
 // Create an instance of the class
 classInstance = classLoaded.newInstance();
System.out.println("Success 2");

Let me know if you need any more details.

JoegalAuthor Commented:

Stack trace looks like:

Exception in thread "main" java.lang.NoClassDefFoundError: xyz/om/main/uto/aClass
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
        at java.lang.reflect.Constructor.newInstance(
        at java.lang.Class.newInstance0(
        at java.lang.Class.newInstance(

Mick BarryJava DeveloperCommented:
> I am sure as the code when run is able to system out "Success" but its not printing "Success2"

printing out the stack trace will verify where the exception is coming from

> ../classes:/usr/j2se/jre/lib/ext/dynamicClass.jar

using a relative entry means you have to run it from the correct directory. Using an absolute dir means you can run it from anywhere.
Its also unecessary to include jar's in the ext directory, they are automatically checked.

Are your classes in packages?

> -Djava.library.path=/projects/classes/mainClass

this aint necessary and not even sure its advisable. whats your reason for it.
Mick BarryJava DeveloperCommented:
> Stack trace looks like:

I see, its finding dynamicClass, but then can't find aClass.
Which does appear to be in a package, so it needs to be in a directory structure matching its package.
Which looks like it should be in:
JoegalAuthor Commented:
I have them in a package as mentioned by you. I have aClass, bClass, mainClass in the path /projects/classes/xyz/om/main/uto

I am really confused why its unable to find them.
JoegalAuthor Commented:
I tried manually creating a directory structure /xyz/om/main/uto under root directory and put the classes under it. It did not work then also.

I also tried creating the directory structure /xyz/om/main/uto under extensions directory and put the classes under it. It did not work.

The only time it works is when I jar the aClass and bClass and put them under extension directory (i.e. the same dir as dynamicClass.jar).

Mick BarryJava DeveloperCommented:
> String className = "dynamicClass";

shouldn't that be:

String className = "";

And just to step back a little can we just use:

new dynamicClass()

and run it with:

java -classpath /projects/classes
Mick BarryJava DeveloperCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoegalAuthor Commented:

I cannot use "new dynamicClass()" as I dont know what the class name would be in advance. Its a "dynamic" one which I have to read run time.

And regarding the String className = "", I am using "" only. Thats why I am able to load it. The problem I am getting is where "" is uanble to find ""


Mick BarryJava DeveloperCommented:
> I cannot use "new dynamicClass()"

it was just for a test.
Mick BarryJava DeveloperCommented:
or try:

Class classLoaded = Class.forName(className);

what do you get when you run:

java -classpath /projects/classes
JoegalAuthor Commented:
I still get the same error:-(
Mick BarryJava DeveloperCommented:
something very strange going on.

can you post the source for all files, and what directory the classes are contained in.
JoegalAuthor Commented:
Sorry I cannot post all files. I will try to provide more details. I appreciate your help.
JoegalAuthor Commented:
I decided to put all my classes in ext directory to resolve the problem as I could not find the way out. Thanks to Objects for help. Hence I would like to grant him the points.
Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.