Jon
Jon

Reputation: 505

My jar file built with ant doesn't run when double click

I've been trying to follow this tutorial over at http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html.

I've got to the part entitled using external libraries, and I was trying to use the log4j2 libraries. When I do ant clean and ant compile jar run in terminal, it launches just fine, however, if I go to the the build/jar folder in my filesystem and double click the jar file, it doesn't open.

Currently, my build file looks like this:

<project name="HelloWorld" 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="main-class"  value="hlw.HelloWorld"/>

<property name="lib.dir"     value="lib"/>

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

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

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false"/>
    <copy todir="${classes.dir}">
        <fileset dir="${src.dir}" excludes="**/*.java"/>
    </copy>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
            <attribute name="Class-Path" value="config/ properties/ ${manifest.classpath}" />
        </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>

<target name="clean-build" depends="clean,jar"/>

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

<target name="test" depends="jar">
    <junit printsummary="yes">
        <classpath>
            <path refid="classpath"/>
            <path refid="application"/>
        </classpath>

        <batchtest fork="yes">
            <fileset dir="${src.dir}" includes="*Test.java"/>
        </batchtest>
    </junit>
</target>

Is there something wrong with this build file that prevents the jar from working when being double clicked?

Edit: when typing jar -jar HelloWorld.jar in Terminal I get the following response:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at hlw.HelloWorld.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

Upvotes: 0

Views: 711

Answers (1)

Piotr Praszmo
Piotr Praszmo

Reputation: 18340

When running your application from ant, you explicitly add jars from lib/ directory to your class-path. When running the jar directly, java has no way of knowing where those jars are.

The simplest way to fix this is to bundle all those dependencies inside single jar:

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

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

If you don't want to repackage those libraries, you can add paths to them in your manifest: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Upvotes: 2

Related Questions