Why does Ant say “NoClassDefFound” when my JAR is

2019-03-04 01:30发布

I am using Java 1.6, Eclipse, and Ant.

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>

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)

Some things that might be useful to know:

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

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

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

标签: java eclipse ant
1条回答
神经病院院长
2楼-- · 2019-03-04 02:05

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

You'll need to do the following:

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

See also ant manifestclasspath task

查看更多
登录 后发表回答