55

I want to convert a .jar to an .exe for microsoft. Is there any program converter for this?

Also if there's one for Mac and Linux I would appreciate suggestions for those too.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
Negrodamus12
  • 559
  • 1
  • 4
  • 4
  • 12
    @Steve Thats on how to use GCJ (which is terrible btw), not on available options. Wrong duplicate – TheLQ Dec 02 '10 at 01:53
  • There is an interesting link in the answers with discussion on the topic including the software mentioned in the answers here. – Steve-o Dec 02 '10 at 01:55
  • 1
    I wish I could give TheLQ reputation for stating that GCJ is terrible. – Tim Bender Dec 02 '10 at 01:57
  • Similar to this [question](https://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file). Try jpackage which is now part of JDK. – DuncG Sep 20 '21 at 11:36

7 Answers7

43

Launch4j works on both Windows and Linux/Mac. But if you're running Linux/Mac, there is a way to embed your jar into a shell script that performs the autolaunch for you, so you have only one runnable file:

exestub.sh:

#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt  0 -a -f "$0" ] && MYSELF="./$0"
JAVA_OPT=""
PROG_OPT=""
# Parse options to determine which ones are for Java and which ones are for the Program
while [ $# -gt 0 ] ; do
    case $1 in
        -Xm*) JAVA_OPT="$JAVA_OPT $1" ;;
        -D*)  JAVA_OPT="$JAVA_OPT $1" ;;
        *)    PROG_OPT="$PROG_OPT $1" ;;
    esac
    shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT

Then you create your runnable file from your jar:

$ cat exestub.sh myrunnablejar.jar > myrunnable
$ chmod +x myrunnable

It works the same way launch4j works: because a jar has a zip format, which header is located at the end of the file. You can have any header you want (either binary executable or, like here, shell script) and run java -jar <myexe>, as <myexe> is a valid zip/jar file.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
16

JSmooth .exe wrapper

JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your Java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a website.

JSmooth provides a variety of wrappers for your java application, each of them having their own behavior: Choose your flavor!

Download: http://jsmooth.sourceforge.net/

JarToExe 1.8 Jar2Exe is a tool to convert jar files into exe files. Following are the main features as describe on their website:

Can generate “Console”, “Windows GUI”, “Windows Service” three types of .exe files.

Generated .exe files can add program icons and version information. Generated .exe files can encrypt and protect java programs, no temporary files will be generated when the program runs.

Generated .exe files provide system tray icon support. Generated .exe files provide record system event log support. Generated windows service .exe files are able to install/uninstall itself, and support service pause/continue.

Executor

Package your Java application as a jar, and Executor will turn the jar into a Windows .exe file, indistinguishable from a native application. Simply double-clicking the .exe file will invoke the Java Runtime Environment and launch your application.

GYaN
  • 2,327
  • 4
  • 19
  • 39
Monny
  • 299
  • 1
  • 3
  • 8
7

If your program is "publicly available non-commercial in nature" and has "a publicly available Web site that meets the basic quality standards", then you can try and get a free license of Excelsior. If its not then it's expensive, but still a viable option.

Program: https://www.excelsiorjet.com

As a side note: Here's a study of all existing Jar to EXE programs, which is a bit depressing - https://www.excelsior-usa.com/articles/java-to-exe.html

Dmitry Leskov
  • 3,233
  • 1
  • 20
  • 17
TheLQ
  • 14,830
  • 14
  • 69
  • 107
  • 2
    Correction: you may get a free Excelsior JET license if your project is non-commercial and available to the general public. It does not matter if it is open source. – Dmitry Leskov Dec 02 '10 at 07:29
  • @Dmitry Ah thanks, habit for me to have open source = public and free. – TheLQ Dec 02 '10 at 12:27
5

Despite this being against the general SO policy on these matters, this seems to be what the OP genuinely wants:

http://www.google.com/search?btnG=1&pws=0&q=java+executable+wrapper

If you'd like, you could also try creating the appropriate batch or script file containing the single line:

java -jar MyJar.jar

Or in many cases on windows just double clicking the executable jar.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • 1
    Just to explain why I posted a Google link, prior to @David's edits, the OPs original question stated specifically "Please don't post telling me this is stupid or asking me why, just give me some links." – Tim Bender Dec 02 '10 at 02:03
  • Also discussed on stackoverflow prior, here: http://stackoverflow.com/questions/3754553/jar-hidden-inside-exe – Steve-o Dec 02 '10 at 02:56
  • You cannot add app specific icon to .bat files, and unless you use "start" the windows cmd will stay open/visible. – guthrie Feb 17 '21 at 13:44
5

I used Launch4J and it works flawlessy,

  • First, Go to your jdk and copy the bin and lib folder
  • Then create a folder for your app and make a folder called jre-(version),
  • Then paste these inside
  • Then open launch4j Put the exe file's path (Inside the app's folder)
  • Then go the jre option and inside bundled paths type jre-(version)
  • Then in minimun version type the version of the java you coded it in.
  • Then in the jre option put "Only use private jdk runtimes"
  • Then hit the gear icon. It will open a file chooser.
  • Choose your application's folder, done
  • Now open the exe file generated. It should work.

In case it does not, try the test run button (The green play one). If it says LinkageError in the console below, then in the jvm-options type --enable-preview. It should work. I hope it helped.

ouflak
  • 2,458
  • 10
  • 44
  • 49
deateaterOG
  • 111
  • 1
  • 3
  • 9
1

if you need to convert from .jar to .exe from java 14 you can used jpackage

jpackage is a command-line tool to create native installers and packages for Java applications.

Update

--type The type of package to create

--input Path of the input directory that contains the files to be packaged

--dest The path where generated output file is placed

--main-jar The main JAR of the application containing the main class

--main-class The qualified name of the application main class to execute.

--module-path The path to modular jars

--add-modules Add a list of modules

--win-shortcut Creates a desktop shortcut for the application.

--win-menu Adds the application to the system menu.

--app-version A version of the application and/or package

--verbose Enables verbose output

--icon Path of the icon of the application package

--jlink-options --bind-services Includes the available service providers that contain the application's main module and all of its dependencies.

Example

jpackage --type msi --app-version "2.0" --input . --dest . --main-jar .\JavaFXHelloWorld.jar --main-class com.example.javafxhelloworld.App --module-path "C:\Program Files\Java\javafx-jmods-17.0.1" --add-modules javafx.controls,javafx.fxml --win-shortcut --win-menu --icon "icon.ico" --name "My App" --jlink-options --bind-services

JPackage documention will help you

  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30030156) – Dom Oct 09 '21 at 00:14
  • 1
    I think `jpackage` just packages your jars into a native installer (i.e. `.msi` or `.deb`, etc.) but the program still needs to be run with `java -jar ...`. I still have to test it though... – Matthieu Dec 21 '21 at 17:03
  • @Matthieu No you don't need to run `java -jar` you just need to run the command – Ahmed Saber Jul 06 '23 at 09:26
  • @Matthieu if u using `--jlink-options --bind-services` u can run your `exe` or `msi` without any java dependency in target machine – Ahmed Saber Jul 06 '23 at 09:48
0

For Windows, you can convert jar to exe using following ways:

  1. Using Netbeans https://erainnovator.com/convert-jar-to-exe-file/
  2. Using Excelsior JET https://youtu.be/iQSfUb8chjg