运行 jar 文件时包含外部 Jar
我正在编写一个 ant 文件,它将编译并运行我的 java 文件。 ant 创建 AntLabRun.jar 文件。当我尝试运行 jar 文件时,我在线程“main”java.lang.NoClassDefFoundError 中收到异常。
这是目录
-lib/resources.jar
-src/**/pkg1/AntLabMain.java
AntLabMain 导入在 resources.jar 中找到的包
<target name="init">
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="apidoc" location="apidoc"/>
<property name="lib" location="lib"/>
<path id="path.base">
<pathelement path="${build}" />
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</path>
<manifestclasspath property="manifest.classpath" jarfile="resources.jar">
<classpath location="${lib}"/>
</manifestclasspath>
</target>
<!-- I excluded the unnecessary targets -->
target name="jar" depends="compile">
<jar destfile="AntLabRun.jar" basedir="${build}">
<include name="edu/**/*.class"/>
<manifest>
<attribute name="Main-Class" value="edu.gatech.oad.antlab.pkg1.AntLabMain"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="AntLabRun.jar" fork="true">
</java>
</target>
的粗略外观我已经浏览了所有与此相关的问题,并且没有提交解决方案尚未。
I am writing an ant file that will compile and run my java files. The ant creates the AntLabRun.jar file. When I try to run the jar file, I get the Exception in thread "main" java.lang.NoClassDefFoundError.
Here is rough look of the directory
-lib/resources.jar
-src/**/pkg1/AntLabMain.java
AntLabMain imports a package found inside resources.jar
<target name="init">
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="apidoc" location="apidoc"/>
<property name="lib" location="lib"/>
<path id="path.base">
<pathelement path="${build}" />
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</path>
<manifestclasspath property="manifest.classpath" jarfile="resources.jar">
<classpath location="${lib}"/>
</manifestclasspath>
</target>
<!-- I excluded the unnecessary targets -->
target name="jar" depends="compile">
<jar destfile="AntLabRun.jar" basedir="${build}">
<include name="edu/**/*.class"/>
<manifest>
<attribute name="Main-Class" value="edu.gatech.oad.antlab.pkg1.AntLabMain"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="AntLabRun.jar" fork="true">
</java>
</target>
I have looked through all of the questions dealing with this and didn't file a solution yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
pathelement
元素。示例:然后在使用
javac
元素时引用id
。示例:最好将上述每个目标都放在自己的目标下(无论如何对我来说)。
您还可以在
path
元素下使用多个pathelement
元素来引用多个 jar。如果您希望您的 jar 文件引用另一个 jar 文件,您可以将引用放在 MANIFEST.MF 中,下面是使用 ant 创建清单的示例。
接下来是示例
target
,用于在创建jar
时复制并包含类文件并指向正确的清单。您会注意到,
depends
标记具有create_manifest
和copy_all_class_files
作为依赖项。如您所知,依赖项目标将首先针对
target
运行,这样您就可以确保顺序正确。我认为你的问题的一部分是你的 ant 文件的复杂性。
尝试将每个任务分解为单独的目标,并在继续下一个目标之前解决每个目标的问题。
这样您就可以确保您之前拥有的任何内容都按预期工作,而不必担心以前的任务无法按预期工作。
You can make use of the
pathelement
element. Example:Then reference the
id
when using thejavac
element. Example:It is better to put each of the above under their own
targets
, (for me anyway).You can also use multiple
pathelement
elements under thepath
element for more than one jar references.If you want your jar file to reference another jar file, you can put the references in the MANIFEST.MF, below is an example creating a manifest using ant.
To follow is Example
target
s to copy and include the class files and point to the correct manifest when creating thejar
.You will note that the
depends
tag has thecreate_manifest
andcopy_all_class_files
as dependency.As you know, the dependency targets will run first for a
target
, that way you can ensure your order is correct.I think part of your problem is your ant file complexity.
Try breaking each of your tasks into separate targets and solving the problem for each target before moving on to the next.
That way you can ensure that whatever prior stuff you have is working as expected and won't have to worry about previous tasks not working as expected.