How to create a Java application which can be run by a click?

前端 未结 8 559
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 15:20

I would like to have a Java application which can be easily started.

So far I have managed to create a jar file but I do not see any advantages yet. Before I run my

相关标签:
8条回答
  • 2020-12-01 15:55

    I actually really like how maven creates them: http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make

    0 讨论(0)
  • 2020-12-01 15:59

    Making a jar with no dependencies executable is relatively easy. You basically just need to specify in the MANIFEST the main class to use. It can then be started with java -jar ExecutableJar.jar, but most OS support double-click in this case.

    Making a jar that depends on other jar executable is more tricky. You can specify the class path in the MANIFEST, but it still means you need to copy more than one jar to distribute your app. Another way is to "pack" the depending jar in the main jar.

    You can do all this with maven and the maven-assembly-plugin. Use the jar-with-dependencies preconfigured descriptor.

    <configuration>
         <descriptorRefs>
                 <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
    </configuration>
    

    The main class can be specified with something like:

    <archive>
         <manifest>
            <mainClass>org.company.MyClass</mainClass>
         </manifest>
     </archive>
    

    EDIT: A complete pom.xml can be found in this question.

    0 讨论(0)
提交回复
热议问题