tags:

views:

269

answers:

9

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.

Can anybody, pleas, help me with that?

A: 

Here some links that may help u..

JSmooth is a Java Executable Wrapper. It creates native Windows launchers

http://jsmooth.sourceforge.net/

Cross-platform Java executable wrapper

http://launch4j.sourceforge.net/


http://www.captain.at/programming/java/executable-java-application.php
Jeeva S
A: 

You can create an executable jar file, as described here:

http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html

Then users can click on it in the file browser and have the main class start, provided the OS and the user's security settings allow it.

Kilian Foth
I just followed the step by step instruction. In the end I got a jar file which I could successfully run. However, when I open the folder with the jar file in my file browser I see the jar file as an archive. Double click on the archive initiate the extraction of the archive.
Roman
Unfortunately you can't really do anything about file browsers that refuse to auto-run stuff (which, after all, is often a very good idea). Even if you compiled the entire JRE and your program into a huge .EXE, nowadays the file manager may very well refuse to run it because it's a "insecure downloaded file", unless you manage to get your application certified by the OS vendor. It's an arms race that ultimately no one can win.
Kilian Foth
+2  A: 

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.

ewernli
A: 

You have to create an executable jar, with mentioning your main class(ie a public class with main() method in it) in the jar's manifest file as the Main-Class. So when prople double click it or type: jar "your jar name" in command console then they will get it executed.

GK
A: 

Launch4J allows you to make a JAR launchable on many platforms.

Valentin Rocher
+1  A: 

It is called WebStart,

However, it does not perfectly fit your description. It does not provide a self-packed solution which should exists.

PS. I personally believe executable jar is a joke, you can't control the vmargs, can't have a good icon, and you can't even know it is executable until it is being unpack and try to be execute.

Dennis Cheung
A: 

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

Chris J
A: 

See this How to bundle java programs into a single executable .jar file for how to use Ant to create a self executing .jar file.

fuzzy lollipop