I have some trouble with getting a Java application to run in the console and/or with Ant. I know that a lot of starting issues are related to the classpath being not set or incorrectly set, though I m fairly sure I set it correctly, so my search only yielded results on that.
Here is the general setup of my application: classes are in packages model, view and controller. controller.Controller is the class with the main method. I am using objectdb as my JPA provider.
我正在使用Ant编译我的应用程序。
编译后,我可以使用以下脚本从ant运行我的应用程序:
<target name="run" description="default build process">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath" />
</classpath>
</java>
</target>
其中${main class}是控制器。控制器和类路径由/lib和/dist文件夹组成(应用程序的jar文件编译为/dist)
现在,我尝试将/lib和/dist中的所有.jar文件复制到一个单独的文件夹中,并使用java-jarcooking.jar-cp运行它们代码>,结果
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
所以我尝试了ant,并对上面的构建目标进行了轻微修改:
<target name="run2" description="default build process">
<java fork="true" jar="${dist.dir}/${ant.project.name}.jar">
<classpath>
<path refid="classpath" />
</classpath>
</java>
</target>
这导致相同的误差。我不明白为什么。
为了测试它,我尝试通过直接指定主类从命令行运行:<code>java-cp。controller.controller,由于某种原因,它甚至无法定位类(它在那里,我确认了它):
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 have set JAVA_HOME to my JDK s path, and CLASSPATH to my JRE s/lib path. OS is Windows 7 64 bit, Java version is 1.6.0_25-b06
I am puzzled by two things: a) Why is Java unable to locate controller.Controller, even though 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 lookup mechanisms.
非常感谢您的帮助。