• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help resolving my NoClassDefFoundError  RSS feed

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is asked a lot, but I didn't see a reply that got me to a solution.

I started with this code:



I have a command prompt open and I compiled it and ran it.

C:\Users\ChemModeling\Documents\FreeThink\Java\InterfaceTests\license>java TestLicense
Expiration Date: 2012-09-30
License is good
25 days left

But I want to understand packages better, so made a License directory and associated sub-directories and created a jar file. Here is how I modified my code:

Basically, I just added the package statement.



If I try to run my jar file (from the same cmd window):

java -jar TestLicense.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/verhas/licensor/License
at freethink.license.TestLicense.checkLicense(TestLicense.java:35)
at freethink.license.TestLicense.main(TestLicense.java:65)
Caused by: java.lang.ClassNotFoundException: com.verhas.licensor.License
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 2 more

It's the same cmd window, the CLASSPATH is set:

echo %CLASSPATH%
.;C:\;C:\Program Files\R\R-2.13.2\library\rJava\jri\JRI.jar;C:\Program Files\R\R
-2.13.2\library\rJava\jri\JRIEngine.jar;C:\Program Files\R\R-2.13.2\library\rJav
a\jri\REngine.jar;C:\Program Files\JFreeChart\lib\jcommon-1.0.16.jar;C:\Program
Files\JFreeChart\lib\jfreechart-1.0.13.jar;C:\Program Files\JFreeChart\lib\jfree
chart-1.0.13-swt.jar;C:\Program Files\JFreeChart\lib\swtgraphics2d.jar;C:\Progra
m Files\JFreeChart\lib\servlet.jar;C:\Program Files\JFreeChart\lib\junit.jar;C:\
Program Files\JFreeChart\lib\gnujaxp.jar;C:\Program Files\JFreeChart\lib\jfreech
art-1.0.13-experimental.jar;C:\Program Files\JFreeChart\lib\iText-2.1.5.jar;C:\U
sers\ChemModeling\Documents\Java\PDFjet\PDFjet-v3.05\PDFjet.jar;C:\Users\ChemMod
eling\Documents\Java\JavaHelp\javahelp2_0_05\jh2.0\javahelp\lib\jh.jar;C:\Users\
ChemModeling\Documents\FreeThink\Java\gui\prototype\asaphelp.jar;C:\Users\ChemMo
deling\Documents\FreeThink\Java\gui\main;C:\Users\ChemModeling\Documents\Java\Li
cense3j\license3j-1.0.3-bin\license3j-1.0.3.jar;C:\Users\ChemModeling\Documents\
Java\License3j\license3j-1.0.3-bin\lib\bcpg-jdk15-143.jar;C:\Users\ChemModeling\
Documents\Java\License3j\license3j-1.0.3-bin\lib\bcprov-jdk15-143.jar;C:\Users\C
hemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\activation-1.1.jar;
C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\jms-1.1.j
ar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\jmxri-
1.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\j
mxtools-1.2.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3
-bin\lib\log4j-1.2.15.jar;C:\Users\ChemModeling\Documents\Java\License3j\license
3j-1.0.3-bin\lib\mail-1.4.jar

I'm not getting something about using other packages in my package. But I'm a bit stumped as to what I'm not getting.
 
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding package can you run it?
>java TestLicense

I think you have changed package in TestLicense, but didn't managed your imports. Are you using eclipse?
 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just running javac from the cmd prompt. I am not using Eclipse.

My directory structure is:


from License\classes I type:

javac -d . ../source/freethink/license/TestLicense.java

(I only have one file for the test) and it creates TestLicense.class in License\classes\freethink\licenses.

Then I run

jar -cfm TestLicense.jar manifest.txt freethink keys

java -jar TestLicense.jar

This gives the error about noClassDefFoundError.

If I remove the package statement and don't make a jar file, then I can create a class file that I just run using java TestLicense and it runs fine. So I think I am just missing some knowledge of how to create my jar file when I am importing packages from other jar files or how to create a package that uses other packages. I've created other jar files no problem. Those jar files use classes that are all in the package I define or classes in java or javax.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got such a big system classpath? That is almost certainly a mistake, since you need a different classpath for each application. I suggest you delete that classpath altogether, unless something has added itself when you installed it (eg QuickTime).
Write down very simply on a piece of paper:
  • The path of your current directory.
  • The path to the directory the class you are seeking is actually in.
  • There ought to be a com directory with a verhas directory inside it and licensor inside that. And their .class file (at least) should be inside that licensor directory. If verhas etc is inside a .jar, you need to provide the classpath to that .jar.
     
    Jon Swanson
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I might need a little more help here. I have written one package. So as far as what I have written, my plan is to make one jar file and so I will have one entry in the class path as a result of my efforts. Actually two, as I have a jar file for my Java help.

    So why the huge CLASSPATH?

    1. I am using a plotting library to make my graphs. The classes/methods are contained in 9 jar files.
    2. I am using a statistics library. The classes/methods are contained in 3 jar files.
    3. I am using a library for pdf writing. This is 1 jar file.
    4. The licensing library I am wanting to use is contained in 9 jar files.

    So there are 22 jar files for classes/methods other people wrote that I need to use in my application. So I put them all in my CLASSPATH. They aren't all needed for this test, but they will be needed for the final project.

    If I just compile my TestLicense.java, don't make it a package and don't put it in a jar file, TestLicense runs.



    works and I can use the statement:



    as License is a class in one of the jar files in the CLASSPATH

    Now I basically just add the line:



    recompile (which gives no errors) and put my class in a jar file, and I get the NoClassDefFoundError. Same CLASSPATH, same import statement, same line of code.

    I must be missing something about either making a package or making a jar file. I'm not sure what.
     
    Jon Swanson
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried simplifying things.



    javac TestLicense.java
    java TestLicense

    WORKS

    jar cfm TestLicense.jar manifest.txt TestLicense.class

    java -jar ./TestLicense.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: com/verhas/licensor/License
    at TestLicense.checkLicense(TestLicense.java:9)
    at TestLicense.main(TestLicense.java:15)
    Caused by: java.lang.ClassNotFoundException: com.verhas.licensor.License
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 2 more

    my manifest.txt



    I put the class in a jar file and get the noClassDefFound error. No other changes. I'm not sure where to go from here to resolve this problem.
     
    Jon Swanson
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think my problem is related to understanding the difference between resolving class references. I added these lines:



    The former line returns the CLASSPATH I defined whether I am running

    java TestLicense
    or
    java -jar TestLicense.jar

    The latter line only returns the CLASSPATH I defined when I run

    java TestLicense

    when I run java -jar TestLicense.jar all I get back from the second line is

    TestLicense.jar

    I also tried

    java -cp ".;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\license3j-1.0.3.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\bcpg-jdk15-143.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\bcprov-jdk15-143.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\activation-1.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\jms-1.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\jmxri-1.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\jmxtools-1.2.1.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\log4j-1.2.15.jar;C:\Users\ChemModeling\Documents\Java\License3j\license3j-1.0.3-bin\lib\mail-1.4.jar" -jar TestLicense.jar

    (the minimum list of jar files that I need to run License)

    and this does not work either. I was really hoping that would work.

     
    Jon Swanson
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I guess I am making progress. This is my current understanding of referencing classes in other jar files if you want to run java -jar myapp.jar:

    1. CLASSPATH is not recognized when running java -jar

    2. -cp and -classpath are not recognized when running java -jar

    3. The only viable option is to define Class-Path: in the manifest file, keeping in mind these caveats:

    a. Unlike CLASSPATH, the directories in Class-Path are relative to the location of your jar file (myapp.jar above)
    b. Use spaces between entries in Class-Path
    c. You may need to put the jar file specifications on separate lines. Start each line with a space.
    A single line is of limited length, I was seeing "line too long" when I had more than 486 characters in a single line,
    docs say lines should not be longer than 72 characters
    I tried using lib/* to specify all jar files in the lib directory, but that did not work
    d. You can't have multiple Class-Path: entries in a manifest file, but one entry can span multiple lines see (c)

    4. If you just need a few classes, you can use a ClassLoader and load them from your program.

    Is this right? Could someone with more experience correct any mistakes here?

    It's turning out to be a lot more work taking my directory of classes and making a jar file than I originally thought (I've already changed the way I read in files and images). Is there any fundamental advantage to a jar file over just putting a directory of class files in CLASSPATH and running java mymain? In my case I call an external program, so no matter what I do, everything won't be in one file that I can just double-click to start.
     
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Java application launcher.


    -jar
    Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
     
    Jon Swanson
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. I had been getting my code working before putting it in a jar file. I see my installer will have a bit of work to do, as the user might already have installed one of the applications I am using, which contains a jar file for its Java interface. So rather than just find where that application was installed and add to CLASSPATH, it sounds like I should copy (or link) the jar files that are contained there into a sub-directory of my application. The reason being that the manifest files locate everything relative to the location of the jar file containing my main and I can't even be sure this other program will be on the same drive.

    I am a bit curious about one thing. I understand that if I correctly define Class-Path and Main-Class in my manifest and create a jar file, I can run it by clicking on it. In my case, I am running a batch file to set the environment of some of the executables my program calls. So adding a line to the batch file 'java mymain' is not a big deal. I also convert the batch file to an exe, allowing me to give it a custom icon.

    Are there other advantages to using a jar file over having the installer create a directory with my class files in it besides double clicking to start the program?
     
    Ranch Hand
    Posts: 297
    Eclipse IDE Firefox Browser Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Hi John,

    I am not sure about your particular case but these post may help you in general :

    http://javarevisited.blogspot.sg/2011/06/noclassdeffounderror-exception-in.html
    http://javarevisited.blogspot.sg/2011/01/how-classpath-work-in-java.html

    I see you already got good understanding of classpath which is must to troubleshoot this kind of error. One more thing which might help you, If you have Classpath attribute defined in Manifest file then that will take preference over -cp and -classpath or CLASSPATH environment variable. If you shipping your application than include all library and try using Class-Path in manifest file.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!