运行 jar 文件时包含外部 Jar

发布于 2024-11-16 07:38:04 字数 1547 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2024-11-23 07:38:04

您可以使用 pathelement 元素。示例:

    <!-- Define the CLASSPATH -->
<path id="compile.classpath">
    <pathelement location="lib/resources.jar" />
</path>

然后在使用 javac 元素时引用 id。示例:

<javac srcdir="${src.home}" destdir="${work.home}/bin" debug="on">
    <classpath refid="compile.classpath" />
</javac>

最好将上述每个目标都放在自己的目标下(无论如何对我来说)。

您还可以在 path 元素下使用多个 pathelement 元素来引用多个 jar。

如果您希望您的 jar 文件引用另一个 jar 文件,您可以将引用放在 MANIFEST.MF 中,下面是使用 ant 创建清单的示例。

<target name="create_manifest">
        <manifest file="${work.home}/META-INF/MANIFEST.MF">
            <attribute name="Manifest-Version" value="1.0" />
            <attribute name="Version" value="${app.version}" />
            <attribute name="Company" value="Comp Name here" />
            <attribute name="Project" value="${app.name}" />
            <attribute name="Java-Version" value="${java.version}" />

<!--Here is the reference to jar files this jar manifest will reference.-->
            <attribute name="Class-Path" value="one.jar two.jar three.jar etc.jar" />
        </manifest>
    </target>

接下来是示例 target,用于在创建 jar 时复制并包含类文件并指向正确的清单。

<target name="create_jar" depends="create_manifest, copy_all_class_files">
    <jar destfile="${guiJar}" manifest="jar_temp/META-INF/MANIFEST.MF" basedir="jar_temp">
    </jar>
</target>

<target name="copy_all_class_files">
    <copy todir="jar_temp">
        <fileset dir="classes">
            <include name="*/**" />
        </fileset>
    </copy>
</target>

您会注意到,depends 标记具有 create_manifestcopy_all_class_files 作为依赖项。

如您所知,依赖项目标将首先针对 target 运行,这样您就可以确保顺序正确。

我认为你的问题的一部分是你的 ant 文件的复杂性。

尝试将每个任务分解为单独的目标,并在继续下一个目标之前解决每个目标的问题。

这样您就可以确保您之前拥有的任何内容都按预期工作,而不必担心以前的任务无法按预期工作。

You can make use of the pathelement element. Example:

    <!-- Define the CLASSPATH -->
<path id="compile.classpath">
    <pathelement location="lib/resources.jar" />
</path>

Then reference the id when using the javac element. Example:

<javac srcdir="${src.home}" destdir="${work.home}/bin" debug="on">
    <classpath refid="compile.classpath" />
</javac>

It is better to put each of the above under their own targets, (for me anyway).

You can also use multiple pathelement elements under the path 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.

<target name="create_manifest">
        <manifest file="${work.home}/META-INF/MANIFEST.MF">
            <attribute name="Manifest-Version" value="1.0" />
            <attribute name="Version" value="${app.version}" />
            <attribute name="Company" value="Comp Name here" />
            <attribute name="Project" value="${app.name}" />
            <attribute name="Java-Version" value="${java.version}" />

<!--Here is the reference to jar files this jar manifest will reference.-->
            <attribute name="Class-Path" value="one.jar two.jar three.jar etc.jar" />
        </manifest>
    </target>

To follow is Example targets to copy and include the class files and point to the correct manifest when creating the jar.

<target name="create_jar" depends="create_manifest, copy_all_class_files">
    <jar destfile="${guiJar}" manifest="jar_temp/META-INF/MANIFEST.MF" basedir="jar_temp">
    </jar>
</target>

<target name="copy_all_class_files">
    <copy todir="jar_temp">
        <fileset dir="classes">
            <include name="*/**" />
        </fileset>
    </copy>
</target>

You will note that the depends tag has the create_manifest and copy_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文