• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the use of external library from a jar archive

 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean.
Are you nesting the jarfiles?
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running your jar? Using java -jar myJar.jar?
Is your jar in the same directory with the imported jar?
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic