Pregunta

I am working on an java project, which uses classes from an external jar. When I build it with ant, it's compiling without any errors. But when i run the jar I will get an "Exception in thread "main" java.lang.NoClassDefFoundError: data/representation/IPlayer". The class IPlayer is in the external jar.

Can you help me please?

my ant file

<?xml version="1.0"?>
<project name="KDL" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="${basedir}/neuerVersuch/version2/6.3/src" />
<property name="build.dir" location="${basedir}/bin" />
<property name="dist.dir" location="${basedir}/dist" />
<property name="doc.dir" location="${basedir}/doc" />
<property name="lib.dir" location="${basedir}/lib" />
<property name="images.dir" location="${basedir}/images" />


<path id="project.classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar" />
    </fileset>
</path>


<!-- Deletes the existing build, docs and dist directory -->
<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${doc.dir}" />
    <delete dir="${dist.dir}" />
</target>

<!-- Creates the build, docs and dist directory -->
<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${doc.dir}" />
    <mkdir dir="${dist.dir}" />
</target>

<!-- Compiles the java code (including the usage of library for JUnit) -->
<target name="compile" depends="clean, makedir">
    <javac destdir="${build.dir}" includeantruntime="false">
        <compilerarg value="-Xlint:unchecked" />
        <src path="${src.dir}" />
        <classpath refid="project.classpath" />
    </javac>
</target>

<!-- Creates Javadoc -->
<target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${doc.dir}">
        <!-- Define which files / directory should get included, we include all -->
        <fileset dir="${src.dir}">
            <include name="**" />
        </fileset>
    </javadoc>
</target>

<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
    <copy todir="${build.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.*" />
        </fileset>
    </copy>
    <jar destfile="${dist.dir}/team1-kdl.jar" basedir="${build.dir}">
        <zipfileset dir="${lib.dir}" includes="*.jar" prefix="libs" />
        <zipfileset dir="${lib.dir}" includes="*.*" prefix="images" />

        <manifest>
            <attribute name="Main-Class" value="controller.GameStart" />
        </manifest>
    </jar>
</target>

<target name="main" depends="compile, jar, docs">
    <description>Main target</description>
</target>

</project>

on the command line I am doing this:

 ant
 java -jar dist/team1-kdl.jar

and then I get these error messages:

Exception in thread "main" java.lang.NoClassDefFoundError: data/representation/IPlayer
    at controller.GameStart.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: data.representation.IPlayer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more

If I press the run button in eclipse, everything works fine

EDIT: Ok, with the answer of JB Nizet it was pretty easy. Just add

<attribute name="Class-Path" value="${lib.dir}/team2.jar" />

to the Manifest Tag in the jar target so it looks like

<manifest>
    <attribute name="Main-Class" value="controller.GameStart" />
    <attribute name="Class-Path" value="${lib.dir}/team2.jar" />
</manifest>

and everything will work

¿Fue útil?

Solución

It looks like you're creating a jar containing other jars, and expect java to automatically add al thosee nested jars in the classpath of your application. That's not how it works. When you start an application using java -jar foo.jar the classpath is composed of

  • foo.jar
  • the list of jars, outside of foo.jar, that are referenced in the Class-Path manifest entry.

Read the tutorial.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top