How to create/convert a Java program to an executable .exe file?
Recently, I used Java Swing to create a graphical user interface (GUI) for my Java program. After I built the program, I would like to convert it to an executable file so that I can directly run it on Windows PC or Mac with JRE installed.
1. How to Create JARs with IDE (e.g. IntelliJ IDEA)
1. What is a JAR file?
A JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.[3]
JAR files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a
.jar
file extension.[4]
2. Executable JAR files
An executable Java program can be packaged in a JAR file, along with any libraries the program uses.
Native launchers can be created on most platforms.
- For example, Windows users who prefer having Windows EXE files can use tools such as JSmooth, Launch4J, WinRun4J or Nullsoft Scriptable Install System to wrap single JAR files into executables.
For me, I used Launch4J to wrap JAR files into executable files.
3. Build JAR files with IntelliJ IDEA
File
-> Projects Structure
-> Project Settings
-> Artifacts
-> Click plus sign
-> Jar
-> From modules with dependencies…
-> Select Main Class
-> Extract to the target Jar
->OK
The generated Jar file can be found following the path: ProjectName/out/artifacts/ProjectName_jar/ProjectName.jar
2. Convert a Java Program to an .exe File
Youtube Tutorial: How to convert jar to exe using Launch4J Full explanation
1. Download Launch4j from http://launch4j.sourceforge.net/
2. Open launch4.exec file
3. Choose the name and path of output .exe file
4. Select the Jar file
5. In JRE section, set the MIN and MAX JRE version (optional)
6. Click [Save Configuration], and, choose the name and path of the .xml file
7. Click [Test Wrapper] to test the executable file
8. The executable will be generated in the same directory as .xml file
3. Convert a Java Program to an .app Application for Mac OS
Youtube Tutorial: How to convert .jar to .app on Mac - a Java tutorial
1. Open Automator in Applications/Utilities
2. Choose “Application”
3. Search for “applescript” and drag it to the workspace
4. Use the code below to replace the original code
Change the name “YOURJARFILE.jar” to the name of your .jar file
on run {input, parameters} set p to POSIX path of (path to me) do shell script "java -jar " & p & "/Contents/Java/YOURJARFILE.jar" end run
If you run the script, some syntax errors may occur. Ignore them.
5. Save your application
6. Go to “Contents ” and make a new folder “Java”
7. Drag your .jar file to the “java” folder
8. You can now open your .app file