English 中文(简体)
Java NoClassDefFoundError尽管设置了类路径
原标题:Java NoClassDefFoundError despite set classpath

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.

非常感谢您的帮助。

问题回答

类路径应包括

  1. directories with class files (in their proper package directory)
  2. jar files.

不能将类路径指向jar目录。当运行应用程序服务器(例如Tomcat)时,情况会有所不同,它将从目录中为您加载jar。

尽管我很确定我设置正确

证据对你不利。JVM告诉您没有正确设置它。

你认为ref类路径指向什么?你认为它的价值观来自哪里?它们应该在Ant build.xml中定义,对吧?像这样:

<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>

如果您正在创建一个可执行JAR,您需要在清单中指定主类和类路径,正如CoolBeans在注释中正确指出的那样。第三方JAR位置必须相对于可执行JAR。您应该将它们与您的可执行JAR打包在一起,这样相对路径就很容易排序和理解。

我发现当我指定<strong><code><;classpath>和目标中的jar=“…”。我删除了jar=“…”,将.jar放入<;classpath>列表,然后运行。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签