131

Possible Duplicate:
How do I create an .exe for a Java program?

I've just made a simple program with Eclipse and I want to compile it into an executable, but simply can't seem to find out how to do it.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Meir
  • 12,285
  • 19
  • 58
  • 70
  • 11
    This MUST be a duplicate :) – Thorbjørn Ravn Andersen Jan 06 '10 at 09:38
  • 1
    very much so: http://stackoverflow.com/questions/516399/create-exe-for-java-program http://stackoverflow.com/questions/309329/best-free-tool-to-build-an-exe-from-java-code http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file http://stackoverflow.com/questions/743029/how-to-get-executable-file-of-java-programm – the.duckman Jan 06 '10 at 10:05
  • 1
    Also dupe of http://stackoverflow.com/questions/1973431/how-do-i-make-an-exe-in-netbeans-java-program and http://stackoverflow.com/questions/2305762/how-to-create-a-jar-exe-of-java-project and http://stackoverflow.com/questions/2855937/how-can-i-convert-java-to-exe – Pops Feb 10 '11 at 19:50

7 Answers7

144

You can convert .jar file to .exe on these ways:
alt text
(source: viralpatel.net)

1- 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 web site.

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

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

2- JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files. Following are the main features as describe in 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 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.
  • New release of x64 version, can create 64 bits executives. (May 18, 2008)
  • Both wizard mode and command line mode supported. (May 18, 2008)

Download: http://www.brothersoft.com/jartoexe-75019.html

3- 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.

Download: http://mpowers.net/executor/

EDIT: The above link is broken, but here is the page (with working download) from the Internet Archive. http://web.archive.org/web/20090316092154/http://mpowers.net/executor/

4- Advanced Installer
Advanced Installer lets you create Windows MSI installs in minutes. This also has Windows Vista support and also helps to create MSI packages in other languages.
Download: http://www.advancedinstaller.com/ Let me know other tools that you have used to convert JAR to EXE.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
  • 10
    1. last updated 2007 3. invalid link 4. invalid link – Nikolay Kuznetsov Oct 17 '12 at 11:29
  • 1
    JWrapper allows you to convert [java to an exe](http://www.jwrapper.com/java-to-exe-free.html) or otherwise native app on the main platforms (Windows Linux, Mac) for free. It runs on any OS, supports built-in code signing and auto-updating, and it can optionally bundle the JRE in a very small (heavily compressed) package. – AntonyM Feb 20 '13 at 11:54
  • 1
    JarToExe is a 30 day trial... – 0x6B6F77616C74 Jul 26 '13 at 13:00
  • 2
    lets see, JSmooth was last updated in 2007. This is 7 years ago. This is like 1000 years in humans age. JarToExe seems to be still active. But no trial version and costs $145. Executor link is dead. Advanced Installer is for making .MSI, no mention of Java on the site, and it looks like marketing web site. This is a really useful answer. No open source software anywhere. – Nasser Mar 08 '14 at 20:36
  • 6
    There's also Launch4J: http://launch4j.sourceforge.net/ (one of many listed on Dmitry's page) – Dave C Sep 11 '15 at 14:35
  • 1
    i used this answer so much many times. but i did not know who answered it is my colleague. – morteza kavakebi Feb 23 '17 at 19:42
  • upvote for the image – Larry Oct 24 '17 at 20:45
  • visit https://regilanj.wordpress.com/2017/11/19/how-to-convert-java-file-into-executables/ for pictorial representation of converting .java to .exe using JSmooth – Maria Irudaya Regilan J Dec 18 '17 at 04:58
  • 1
    @DmitryLeskov, do you have any plans on updating this list? I suggest couple changes as of April 20, 2018. I.e. RoboVM site is not even opening. GCJ page is also not opening as well. There is some information here https://gcc.gnu.org/wiki/GCJ but it appears it's discontinued. Launch4j's status is (I'd say) "barely active", You can already download JRE 10, but launch4j still don't have support for Java 9 version detection (feature request since 2016 https://sourceforge.net/p/launch4j/feature-requests/107/) and it's not even scheduled for next release. – Sergey Karpushin Apr 21 '18 at 00:40
  • the only issue I see is that all the software is for Windows, the author might requested other OS – Ihor Kolodyuk Sep 23 '21 at 07:40
  • TL;DR do any or all in this list package the JVM with the EXE such that user can simply run EXE and not have to install JVM? I know with python to EXE, the tooling embeds python runtime with the EXE. – David Jun 15 '22 at 21:04
  • 1
    This might be the latest contemporary option? https://www.graalvm.org/native-image/ – David Jun 15 '22 at 21:31
40

I would use GCJ (GNU Compiler for Java) in your situation. It's an AOT (ahead of time) compiler for Java, much like GCC is for C. Instead of interpreting code, or generating intermediate java code to be run at a later time by the Java VM, it generates machine code.

GCJ is available on almost any Linux system through its respective package manager (if available). After installation, the GCJ compiler should be added to the path so that it can be invoked through the terminal. If you're using Windows, you can download and install GCJ through Cygwin or MinGW.

I would strongly recommend, however, that you rewrite your source for another language that is meant to be compiled, such as C++. Java is meant to be a portable, interpreted language. Compiling it to machine code is completely against what the language was developed for.

jakogut
  • 4,409
  • 6
  • 29
  • 41
  • 1
    +1 for being the one comment that (correctly) answers in the affirmative. I don't know if GCJ supports everything the conventional classfile/jvm combo does, but it's definitely worth a try for a "simple program". – Edmund Jan 06 '10 at 08:50
  • @SjB GCJ does compile to native executables that do not require a JVM. – Mark Jan 06 '10 at 08:55
  • 4
    @Mark - they don't require a separate JVM, but the libgcj libraries are effectively a JVM. – Stephen C Jan 06 '10 at 11:00
  • @Edmund - the downside is that GCJ uses Classpath, which is not 100% compatible with Sun's Java class libraries. (The Classpath folks tried their best, but they were hampered by clean-room approach, and Sun's unwillingness to let the project have access to the Java TCK.) – Stephen C Jan 06 '10 at 11:08
  • As far as I know, GCJ does not support Swing. – ceklock Jan 12 '13 at 05:47
  • 1
    > Java is meant to be a portable, interpreted language. I _really_ don't think that Java is _meant_ to be interpreted. – cubuspl42 Jun 16 '16 at 18:55
  • 1
    @cubuspl42: In hindsight, and after learning more over the years, I agree. Languages aren't meant to be executed any way or the other. That's more of an implementation detail. Python is typically interpreted, but compiling it to C doesn't violate any spec I know of. However, It does, in some cases, obviate some of the intended design goals and benefits of the language. If you're looking to use one of your tools in a way that's extraordinary and unconventional, it might be worth thinking about whether or not it's the best tool for the job. – jakogut Jun 17 '16 at 22:47
  • Note: GCJ is dead. – dimitar.bogdanov Jan 10 '21 at 12:14
  • It's not Java, but the Java source code that's meant to be portable and interpreted. Compiling it to machine code doesn't change the portable nature of the source code. And "interpreted" is just an archaic implementation detail. With the advent of JIT compiling, Java ceased to be an interpreted language. – MiguelMunoz Mar 01 '23 at 10:14
18

I use launch4j

ANT Command:

<target name="jar" depends="compile, buildDLLs, copy">
    <jar basedir="${java.bin.dir}" destfile="${build.dir}/Project.jar" manifest="META-INF/MANIFEST.MF" />
</target>

<target name="exe" depends="jar">
    <exec executable="cmd" dir="${launch4j.home}">
        <arg line="/c launch4jc.exe ${basedir}/${launch4j.dir}/L4J_ProjectConfig.xml" />
    </exec>
</target>
oliver31
  • 2,523
  • 3
  • 19
  • 25
5

I usually use a bat script for that. Here's what I typically use:

@echo off
set d=%~dp0
java -Xmx400m -cp "%d%myapp.jar;%d%libs/mylib.jar" my.main.Class %*

The %~dp0 extract the directory where the .bat is located. This allows the bat to find the locations of the jars without requiring any special environment variables nor the setting of the PATH variable.

EDIT: Added quotes to the classpath. Otherwise, as Joey said, "fun things can happen with spaces"

Community
  • 1
  • 1
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
1

We have found Jsmooth to be well-working and easily scriptable with ant under Linux. You may want to use one-jar (also easily scriptable with ant under Linux) to collect a multifile application in a single jar first.

We primarily needed the easy deployment of the EXE combined with the "hey, you need Java version X, go here to download" facilities.

(but what you most likely need is the "Runnable jar" / "Executable jar" facility in standard Java).

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

The thing you can do is create a .bat that will execute the .jar file created, checking if there is a JRE present.

From Mitch useful link (source)

java -classpath myprogram.jar de.vogella.eclipse.ide.first.MyFirstClass

This can be used into your batch...

Vincent B.
  • 524
  • 3
  • 15
0

There is a small handful of programs that do that... TowerJ is one that comes to mind (I'll let you Google for it) but it costs significant money.

The most useful reference for this topic I found is at: http://mindprod.com/jgloss/nativecompiler.html

it mentions a few other products, and alternatives to achieve the same purpose.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167