Java构建与外部JAR文件Ant文件 [英] java build ant file with external jar files
问题描述
我创建了一个Ant构建文件来编译我的Java SRC成一个.jar文件。它使用外部JAR文件。
I created an ant build file to compile my java src into a .jar file. It uses external jar files.
我的文件目录:
+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
The external libraries are in lib folder. The main class is in HelloServer.java
下面是我的Ant构建文件:
Here is my ant build file:
<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>
我执行运行,所有的迪尔斯成功创建,.jar文件被创建,它是能够被执行。
I execute run and all the dirs were created successfully, the .jar file was created and it was able to be executed.
我打开终端进入罐子目录。我试图通过命令行像这样来执行,它给了我以下错误:
I open terminal and go to the jar dir. I tried to execute through command line like this and it gave me the following error:
$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文件,我居然包括外部库。
Why does it say class file not found? I thought by creating the helloworld jar file, i actually included the external libraries.
推荐答案
您将需要使用所有的罐子组合在一起 zipgroupfileset
:
You'll need to combine all the jars together using 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>
请参阅:创建蚂蚁一个包JAR
See: Creating a bundle jar with ant
这篇关于Java构建与外部JAR文件Ant文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!