我创建了一个ant构建文件,将我的java src编译成.jar文件。它使用外部jar文件。
我的文件目录:
+src
----android
------------Address.java
------------HelloServer.java
------------HelloServerResource.java
+lib
------------org.codehaus.jackson.core.jar
------------org.codehaus.jackson.mapper.jar
------------org.json.jar
------------org.restlet.ext.jackson.jar
------------org.restlet.jar
build.xml
外部库位于lib文件夹中。 主类在HelloServer.java
中这是我的ant构建文件:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}" classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
我执行run并且所有dirs都已成功创建,.jar文件已创建并且能够执行。
我打开终端并转到jar目录。我尝试通过这样的命令行执行,它给了我以下错误:
$java -jar helloworld.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/restlet/Server
at android.HelloServer.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.restlet.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
为什么说找不到类文件?我想通过创建helloworld jar文件,我实际上包含了外部库。
答案 0 :(得分:8)
您需要使用zipgroupfileset
将所有广告瓶组合在一起:
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipgroupfileset dir="${lib-dir}" includes="*.jar"/>
</jar>
</target>
答案 1 :(得分:3)
我找到了解决方案。基本上我编译我的jar文件并使用清单文件将其指向外部jar文件。
这是我的构建文件:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}"
classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
主要区别在于我将以下内容添加到目标“jar”中:
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<attribute name="Class-Path" value="${jar.classpath}"/>