Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jar

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 application by "java HelloWorldSwing" and now I use "java -jar HelloWorldSwing.jar" (which is even more complicated than the previous command and, as far as I understood, the usage of a jar file requires presence of *.mf file in the same directory).

I would like to have one of the two following situations:

  1. Just one single file which can be copied to another operation system and that the project can be started just by click on this file in a file browser (at the moment if click on my jar file Ubuntu starts to extract archive (because jar is an archive, I know)).

  2. Create a pictogram which can be put on a desktop and clicking on which initiates my Java program.

like image 512
Roman Avatar asked Feb 18 '10 12:02

Roman


2 Answers

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.

like image 161
ewernli Avatar answered Sep 23 '22 22:09

ewernli


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

like image 44
Chris J Avatar answered Sep 24 '22 22:09

Chris J