简体   繁体   English

蚂蚁在构建可运行的jar时被卡住

[英]Ant gets stuck while building runnable jar

it's me again. 又是我。 I've been trying to create java project into a runnable jar using ant script. 我一直在尝试使用ant脚本将Java项目创建到可运行的jar中。 This is my build.xml 这是我的build.xml

<project name="simple-app" basedir="." default="main">
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="app.App" />

    <path id="classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar" />
    </fileset>
            <dirset dir="${build.dir}">
            <include name="classes"/>
    </dirset>
</path>

<target name="clean">
    <delete dir="${build.dir}" />
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac source="1.7" target="1.7" includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
        </manifest>
    </jar>
</target>


<target name="run" depends="jar">
    <java classname="${main-class}">
        <classpath>
            <path refid="classpath" />
            <path location="${jar.dir}/${ant.project.name}.jar" />
        </classpath>
    </java>
</target>

<target name="main" depends="clean,run"/>
</project>

All goes well, but when it gets to "run" it gets stuck. 一切顺利,但是当它“运行”时会卡住。 Terminal says run: and nothing happens. 终端说运行:什么也没有发生。 Waited for 30 minutes, nothing. 等了30分钟,什么都没有。 I tried many other options I found around the internet but those resulted either in the same lag, or threw ClassNotFoundException. 我尝试了在互联网上找到的许多其他选项,但是这些选项导致了同样的滞后,或者抛出了ClassNotFoundException。

I seriously don't know what, to do. 我真的不知道该怎么办。 When I make the file with Eclipse, all works fine. 当我使用Eclipse制作文件时,一切正常。 Anyone can help me? 有人可以帮助我吗? It's propably something totally stupid, but I just don't see it. 这可能是完全愚蠢的,但我只是看不到。 Thank you very much. 非常感谢你。

To fix the ClassNotFoundException exception you need to include the classpath in the jar's manifest. 要修复ClassNotFoundException异常,您需要在jar的清单中包含类路径。 The manifestclasspath task comes in very useful: manifestclasspath任务非常有用:

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />

    <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}" includes="*.class">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
            <attribute name="Class-Path" value="${jar-classpath}" />
        </manifest>
    </jar>
</target>

This enables you to invoke the jar as follows: 这使您可以按以下方式调用jar:

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

or from the command-line: 或从命令行:

java -jar /path/to/myproject.jar

In the end this may not explain why your build is hanging.... Is it possible the code is going into a waiting state? 最后,这可能无法解释您的构建为何挂起。...代码是否可能进入等待状态?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM