sdasdadas
sdasdadas

Reputation: 25156

Why does Ant say "NoClassDefFound" when my JAR is on the classpath?

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:

Upvotes: 0

Views: 312

Answers (1)

beny23
beny23

Reputation: 35048

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

Upvotes: 2

Related Questions