我正在开发一个java项目,它使用外部jar中的类。当我用ant构建它时,它正在编译而没有任何错误。 但是当我运行jar时,我将得到一个“线程中的异常”主“java.lang.NoClassDefFoundError:data / representation / IPlayer”。 IPlayer类位于外部jar中。
你能帮我吗?
我的蚂蚁文件
<?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>
命令行上的我这样做:
ant
java -jar dist/team1-kdl.jar
然后我收到这些错误消息:
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
如果我在eclipse中按下运行按钮,一切正常
编辑: 好的,有了JB Nizet的答案,这很容易。 只需添加
<attribute name="Class-Path" value="${lib.dir}/team2.jar" />
到jar目标中的Manifest标签,看起来像
<manifest>
<attribute name="Main-Class" value="controller.GameStart" />
<attribute name="Class-Path" value="${lib.dir}/team2.jar" />
</manifest>
一切都会有效
答案 0 :(得分:0)
看起来你正在创建一个包含其他jar的jar,并期望java在你的应用程序的类路径中自动添加嵌套的jar。这不是它的工作原理。使用java -jar foo.jar
启动应用程序时,类路径由
阅读the tutorial。