简体   繁体   中英

How to run .jar file generated with Apache Ant?

I am using IntelliJ IDEA 2016.3.1 with built in Apache Ant 1.9.4. I use the script file given below to build my project. The jar target generates .jar file without any errors. I can run my application from IDE. However, I can't run my application from .jar file. What am I doing wrong? Is it a special way to run .jar files.

<project name="RegexDemo" basedir=".">

<property name="version">1.0.0</property>
<property name="dir.src">src/com/fagan/demo</property>
<property name="dir.build">build</property>
<property name="dir.build.classes">${dir.build}/classes</property>
<property name="dir.build.javadoc">${dir.build}/javadoc</property>
<property name="file.jar">${dir.build}/RegexDemo-${version}.jar</property>

<path id="projectClasspath">
    <fileset dir="lib">
        <include name="**.jar"/>
    </fileset>
</path>

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

<target name="init">
    <mkdir dir="${dir.build}"/>
    <mkdir dir="${dir.build.classes}"/>
</target>

<target name="compile" depends="init">
    <echo>Compiling Java source</echo>
    <javac classpathref="projectClasspath"
           srcdir="${dir.src}"
           destdir="${dir.build.classes}"/>
</target>

<target name="jar" depends="compile">
    <echo>Making JAR file</echo>
    <jar basedir="${dir.build.classes}" file="${file.jar}"/>
</target>

<target name="javadoc">
    <echo>Making JavaDoc from source</echo>
    <javadoc sourcepath="${dir.src}" destdir="${dir.build.javadoc}"/>
</target>

</project>

EDIT: I replaced my jar target with the following snippet, but it didn't work either

<target name="jar" depends="compile">
    <echo>Making JAR file</echo>
    <jar basedir="${dir.build.classes}" file="${file.jar}">
    <manifest>
        <attribute name="Main-Class" value="com.fagan.demo.MainWindow"/>
        <attribute name="Class-Path" value="${file.jar}"/>
    </manifest>
    </jar>
</target>

EDIT 2: Here is my MANIFEST.MF file

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_111-b14 (Oracle Corporation)
Main-Class: com.fagan.demo.MainWindow
Class-Path: build/RegexDemo-1.0.0.jar

Check if the Main-class mentioned in the MANIFEST is present in the jar file and also check if the entry generated by ANT in Manifest is actually present. Also you should use destfile attribute in jar task rather than file attribute.

Correct me if I understood incorrectly, if you are expecting it to run by a simple double click, then it won't run. After making the jar, use the command java -jar to execute it. Alternatively, you can put this command in a batch file/shell script next to jar and then double clicking it will work fine. Hope this helps.

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