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

//WORKS FINE TILL HERE
classInstance = classLoaded.newInstance(); //FAILS (Look at dynamicClass)

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 http://www.javageeks.com/Papers/ClassForName/ClassForName.pdf

But it did not help.

Thanks in advance.

JoegalAsked:
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.

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

what error?

Where is dynamicClass?
0
objectsCommented:
where are you using URLClassLoader?
0
jimmackCommented:
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).
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

JoegalAuthor Commented:
Hi,

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?

Thanks.


0
objectsCommented:
> 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.
0
JoegalAuthor Commented:
Objects,

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());
System.out.println("Success");
//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.

Thanks.
0
JoegalAuthor Commented:
Objects,

Stack trace looks like:

Exception in thread "main" java.lang.NoClassDefFoundError: xyz/om/main/uto/aClass
        at xyz.om.main.uto.dynamicClass.<init>(dynamicClass.java:54)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
        at java.lang.Class.newInstance0(Class.java:308)
        at java.lang.Class.newInstance(Class.java:261)
        at xyz.om.main.uto.mainClass.setClassLoader(mainClass.java:500

Thanks.
0
objectsCommented:
> 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.
0
objectsCommented:
> 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:
/projects/classes/xyz/om/main/uto
0
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.
0
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).


0
objectsCommented:
> String className = "dynamicClass";

shouldn't that be:

String className = "xyz.om.main.uto.dynamicClass";

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

new dynamicClass()

and run it with:

java -classpath /projects/classes xyz.om.main.uto.mainClass
0
objectsCommented:
0

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:
Objects,

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 = "xyz.om.main.uto.dynamicClass", I am using "xyz.om.main.uto.dynamicClass" only. Thats why I am able to load it. The problem I am getting is where "xyz.om.main.uto.dynamicClass" is uanble to find "xyz.om.main.uto.aClass"

Thanks.

0
objectsCommented:
> I cannot use "new dynamicClass()"

it was just for a test.
0
objectsCommented:
or try:

Class classLoaded = Class.forName(className);

what do you get when you run:

java -classpath /projects/classes xyz.om.main.uto.mainClass
0
JoegalAuthor Commented:
I still get the same error:-(
0
objectsCommented:
something very strange going on.

can you post the source for all files, and what directory the classes are contained in.
0
JoegalAuthor Commented:
Sorry I cannot post all files. I will try to provide more details. I appreciate your help.
0
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.
0
objectsCommented:
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.