Executing a Java running application from a Jar or

2019-06-08 16:38发布

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!

3条回答
欢心
2楼-- · 2019-06-08 17:11

Alternate ways to run a jar file

  1. java -jar jar-file

  2. java -cp jar-file MainClassNameFullyQualified

查看更多
趁早两清
3楼-- · 2019-06-08 17:17

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-06-08 17:23

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
查看更多
登录 后发表回答