0

I have a Java application and a build file which, among its tasks, has one task to create a jar file from the application and one to run the application

<!-- ===== Create An Executable Jar target ======-->
<target name="jar-task" depends="compile-task">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="jar/Guix.jar" basedir="${gui_bin.dir}">
        <fileset dir="${basedir}">
            <include name="img/**/" />
            </fileset>
            <manifest>
                <attribute name="Main-Class" value="sys.deep.cepu.Start"/>
            </manifest>
            <filelist dir="${basedir}" files="user.properties"/>
    </jar>
</target>

<!-- 
    ============ Run target =================== 
    -->
<target name="run-task" depends="jar-task">
      <java classpath="${basedir};jar/Guix.jar;CK-DASP-2.0.0.jar;library/*;user.properties;img/* " classname="sys.deep.cepu.Start" fork="true">
    </java>
</target>

The build file runs perfectly, the jar is created and the application runs. I would like to allow the user to start the application by clicking on a single file (jar or batch file). I tried to click on the generated executable jar file but nothing happens. Is it normal? Can someone give me an help on how to execute the program from this jar or from a batch file? Thanks!

Anto
  • 4,265
  • 14
  • 63
  • 113
  • possible duplicate of [Running Jar file in Windows](http://stackoverflow.com/questions/394616/running-jar-file-in-windows) – CoolBeans Oct 07 '11 at 16:44
  • see the duplicate question link above. If that answers your question then you should flag to delete this post. – CoolBeans Oct 07 '11 at 16:44

3 Answers3

3

Yes, this is normal. You have to start it from command line or via batch script. Try using as a batch script (if you have a MANIFEST.MF) added to your jar.

java -ea -jar Application.jar <Parameters>

or otherwise:

java -cp jar-file packageOfMainClass.MainClass
Tobias
  • 9,170
  • 3
  • 24
  • 30
  • It throws the error: Could not find the main class: sys.deep.cepu.Start. Program will exit. Strange because the main class is there and it works with the run task – Anto Oct 07 '11 at 16:48
  • Did you include a MANIFEST.MF in you jar file? – Tobias Oct 07 '11 at 16:49
  • Well, there is a element in the jar-task..is this what you are referring to? – Anto Oct 07 '11 at 16:54
1

When building you jar file you already specify the Main-Class. But you did not specify the required libraries in the manifest file, only in the ant file when running the application.

Write another manifest attribute into the ant-file. The name should be Class-Path, the value a space separated list of libraries. Look here (Wikipedia) for an example.

After that your application should start when entering

java -jar Guix.jar

Then follow the step described in this question to make it startable with a double click.

Community
  • 1
  • 1
A.H.
  • 63,967
  • 15
  • 92
  • 126
0

Alternate ways to run a jar file

  1. java -jar jar-file

  2. java -cp jar-file MainClassNameFullyQualified

sudmong
  • 2,036
  • 13
  • 12