Executing a Java running application from a Jar or Batch file

旧街凉风 提交于 2019-12-23 04:22:01

问题


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!


回答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.




回答2:


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



回答3:


Alternate ways to run a jar file

  1. java -jar jar-file

  2. java -cp jar-file MainClassNameFullyQualified



来源:https://stackoverflow.com/questions/7690213/executing-a-java-running-application-from-a-jar-or-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!