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?

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

1

2

3

4

5

The generated Jar file can be found following the path: ProjectName/out/artifacts/ProjectName_jar/ProjectName.jar

6

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

  • 7

3. Choose the name and path of output .exe file

  • 8

4. Select the Jar file

  • 9
  • 10

5. In JRE section, set the MIN and MAX JRE version (optional)

  • 11

6. Click [Save Configuration], and, choose the name and path of the .xml file

  • 12

7. Click [Test Wrapper] to test the executable file

  • 13

8. The executable will be generated in the same directory as .xml file

  • 14

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

  • 15

2. Choose “Application”

  • 16

3. Search for “applescript” and drag it to the workspace

  • 17

4. Use the code below to replace the original code

  • 18

  • 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

  • 19
  • 20

6. Go to “Contents ” and make a new folder “Java”

  • 21
  • 22

7. Drag your .jar file to the “java” folder

  • 23
  • 24

8. You can now open your .app file

4. References


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

Hello World Next