This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Problem with the use of external library from a jar archive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with the use of external library from a jar archive" Watch "Problem with the use of external library from a jar archive" New topic
Author

Problem with the use of external library from a jar archive

Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Hi all,

I had created a class which uses the functionality of an external library (a jar file). I had set the CLASSPATH variable to denotes the path to the external library and all went well when I complied and run the class. The problem arises when I jarred the class into a jar archive; a java.lang.NoClassDefFoundError denoting that the external library cannot be found. Appreciate any help given.

Regards,
Clivant


Clivant Yeo
My Personal Website
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You need to list jars explicitly in your classpath.
IOW, you can't just point the the directory they're in.

Example
export CLASSPATH=/home/me/java:/home/me/java/lib/myJar.jar

Or in Windows:

set CLASSPATH=C:\me;C:\me\lib\myJar.jar


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Hi Ben,

I did list the jar archive explicitly, that's why the class can run smoothly before it is being jarred up. The problem occurs when I jarred it up and tries to execute it from the new jar file.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure what you mean.
Are you nesting the jarfiles?
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
I am not nesting Jar files. I had a class (Engine) importing a package from a external library (another jar file). I had set the CLASSPATH correspondingly, and my class can run smoothly. After I jarred up my Engine class in a jar file, I cannot run the jar file smoothly; a java.lang.NoClassDefFoundError is thrown, indicating that the package that I imported does not exist.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you sure that the class not found wasn't your own?
Did you add your jar file to the classpath?
Also, check the structure of your jar file with:
jar -tf yourjarfile.jar
to make sure you didn't nest your package structure too deeply.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
How are you running your jar? Using java -jar myJar.jar?
Is your jar in the same directory with the imported jar?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Are you sure that the class not found wasn't your own?
Did you add your jar file to the classpath?
Also, check the structure of your jar file with:
jar -tf yourjarfile.jar
to make sure you didn't nest your package structure too deeply.


Yeah the class not found is not my own. I added my jar file too, but it just won't work. And the structure of my jar file is correct.
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
How are you running your jar? Using java -jar myJar.jar?
Is your jar in the same directory with the imported jar?


I am running my jar file as java -jar myJar.jar in order to see the output. Be it the jar files are in the same directory or not, the same error is thrown.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with the use of external library from a jar archive