0

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
saidfagan
  • 841
  • 2
  • 9
  • 26
  • You want to make a "runnable jar" which is what the name implies. This question might help: http://stackoverflow.com/questions/9874550/how-to-create-a-bundled-runnable-jar-using-ant – Alex Jan 30 '17 at 17:45
  • Thanks for your answer, but that's an unsolved question. However, I tried some suggestions from that question and they didn't work. – saidfagan Jan 30 '17 at 17:57
  • You say it didn't work. Could you indicate what kind of error was thrown? – Mark O'Connor Jan 31 '17 at 00:12
  • Mark, .jar file is created without any error. But when i double click it to run just nothing happens. – saidfagan Jan 31 '17 at 06:09

2 Answers2

1

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.

Mayank
  • 83
  • 1
  • 9
  • I edited my question and added Manifest file content. Also, i replaced 'file="${file.jar}"' with 'destfile="${file.jar}"' in my jar target. All this didn't help. – saidfagan Jan 31 '17 at 06:49
1

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.

Jayesh
  • 402
  • 1
  • 4
  • 22
  • Yes, I was trying to run .jar by double-clicking it. Now, I understand my problem. Thanks for you answer. – saidfagan Jan 31 '17 at 08:31