本文介绍了为什么 Ant 说“NoClassDefFound"?当我的 JAR 在类路径上时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Java 1.6、Eclipse 和 Ant.

I am using Java 1.6, Eclipse, and Ant.

以下是我创建 jar 文件并运行它的目标:

The following is my target for creating a jar file and running it:

    <!-- Settings -->
    <property file="build.properties" />
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <!-- Compile -->
    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
            <classpath refid="classpath" />
        </javac>
    </target>

    <!-- Package .jar -->
    <target name="jar">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/App.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="main.App" />
            </manifest>
        </jar>
    </target>

    <!-- Run .jar -->
    <target name="run">
        <java jar="${jar.dir}/App.jar" fork="true" />
    </target>

问题是,当我运行这个 jar(通过 Ant 或命令行)时,我收到错误:

The problem is that when I run this jar (via Ant or command-line) I receive the error:

Exception in thread "main" java.lang.NoClassDefFoundError: net/xeoh/plugins/base/impl/PluginManagerFactory
     [java]     at plugins.PluginLoader.<clinit>(Unknown Source)

一些可能有用的知识:

  • 当我打印我的类路径时,它显示所有必需的 JAR 都是那里;它还显示了类路径的 Eclipse GUI 版本.

  • When I print my classpath, it shows that all the requisite JARs arethere; it also shows up the Eclipse's GUI version of the class path.

我尝试清理项目(通过 Eclipse 和 Ant)但无济于事.

I have tried cleaning the project (both via Eclipse and Ant) to no avail.

似乎缺少的库 .jar 不是 .jar 中的 .jar(这似乎是一个常见问题).

The library .jar that seems to be missing is not a .jar in a .jar (which seems to be a common problem).

这是唯一的错误.其他班级似乎找到了图书馆……

This is the only error. Other classes seem to find the library alright...

推荐答案

您已经设置了编译类路径,但 App.jar 不包含您的库(仅包含您编译的类)或清单类路径.

You've set the compile classpath but the App.jar does not include your libs (only the classes you compiled) or a manifest classpath.

您需要执行以下操作:

<target name="jar">
    <mkdir dir="${jar.dir}" />
    <manifestclasspath property="manifest.classpath"
                       jarfile="${jar.dir}/App.jar">
      <classpath refid="classpath" />
    </manifestclasspath>
    <jar destfile="${jar.dir}/App.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="main.App" />
            <attribute name="Class-Path" value="${manifest.classpath}" />.
        </manifest>
    </jar>
</target>

另见ant manifestclasspath任务

这篇关于为什么 Ant 说“NoClassDefFound"?当我的 JAR 在类路径上时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:23