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.