简体   繁体   中英

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. This is my 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. I tried many other options I found around the internet but those resulted either in the same lag, or threw ClassNotFoundException.

I seriously don't know what, to do. When I make the file with Eclipse, all works fine. 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. The manifestclasspath task comes in very useful:

<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:

<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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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