Java NoClassDefFoundError, despite the given classpath

I have some problems with running a Java application in the console and / or using Ant. I know that many problems with starting are related to the fact that the class path is not set or is incorrectly installed, although I am sure that I installed it correctly, so my search returned only the results.

Here is the general setup of my application: the classes are in the package model, view, and controller. controller.Controller is a class with a main method. I use objectdb as a JPA provider.

I use Ant to compile my application.

After compilation, I can run the application from Ant with the following script:

<target name="run" description="default build process"> <java fork="true" classname="${main-class}"> <classpath> <path refid="classpath" /> </classpath> </java> </target> 

where $ {main-class} is the controller. Controller and classpath consists of / lib and / dist folders (the jar file of the application file is compiled into / dist)

Now I tried to copy all .jar files from / lib and / dist into one separate folder and run them with java -jar cooking.jar -cp . , that leads to

 Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence at model.jpa.JPAModelFactory.<init>(JPAModelFactory.java:28) at model.jpa.JPAModelFactory.<init>(JPAModelFactory.java:24) at controller.Controller.<init>(Controller.java:59) at controller.Controller.main(Controller.java:116) Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 4 more 

So, I tried Ant and slightly modified the build target above:

 <target name="run2" description="default build process"> <java fork="true" jar="${dist.dir}/${ant.project.name}.jar"> <classpath> <path refid="classpath" /> </classpath> </java> </target> 

which leads to the same error. I do not understand why.

Just to test it, I tried to run it from the command line, specifying the main class directly: java -cp . controller.Controller java -cp . controller.Controller , which for some reason cannot even find the class (it is there, I confirmed it):

 Exception in thread "main" java.lang.NoClassDefFoundError: controller/Controller Caused by: java.lang.ClassNotFoundException: controller.Controller at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: controller.Controller. Program will exit. 

I installed JAVA_HOME on my JDK path and CLASSPATH on my JRE / lib path. OS - 64-bit version of Windows 7, Java version - 1.6.0_25-b06

I am puzzled by two things: a) Why Java cannot find the controller. The controller, even if it is present in the .jar file, and the .jar file is in the current directory? b) What am I doing wrong, that calling Java with -jar seems to mess up the search engines.

Any help is appreciated.

+8
java classpath noclassdeffounderror
source share
3 answers

The class path must consist of

  • with class files (in the corresponding package directory)
  • Jar files.

You cannot specify the directory path in jars. When you start the application server (for example, Tomcat), everything will be different, which will load banks from the directory for you.

+2
source share

although I'm sure I installed it correctly

The evidence is against you. The JVM tells you that you did not install it correctly.

What do you think ref 'classpath' points to? Where do you take its values ​​from? They must be defined inside Ant build.xml, right? Like this:

 <path id="production.class.path"> <pathelement location="${production.classes}"/> <pathelement location="${production.resources}"/> <fileset dir="${production.lib}"> <include name="**/*.jar"/> <exclude name="**/junit*.jar"/> <exclude name="**/*test*.jar"/> </fileset> </path> <path id="test.class.path"> <path refid="production.class.path"/> <pathelement location="${test.classes}"/> <pathelement location="${test.resources}"/> <fileset dir="${test.lib}"> <include name="**/junit*.jar"/> <include name="**/*test*.jar"/> </fileset> </path> 

If you are creating an executable JAR, you need to specify the main class and the class path in the manifest, as indicated in the CoolBeans comment. The locations of the third-party JARs must be relative to the executable JAR. You must package them with an executable JAR in such a way that the relative path is easy to figure out and understand.

+1
source share

I found this to happen when I specify both <classpath> and jar="..." in the target. I removed jar="..." , put this .jar in the <classpath> list, and it started after that.

0
source share

All Articles