Technoloxy

Daily Source of Tech

Tutorials

JAVA to EXE: The Complete Guide to executable files!

JAVA image

JAVA imageHave a .java file/program that you would like to run outside of JAVA’s environment, change it from java to exe? You have come to the right place! 

This is the complete guide to convert a JAVA file to JAR file and then convert the JAR file to EXE file which will result your JAVA program to become executable.

It is a simple follow along guide, you do not require any knowledge on the programs used here. Two main programs used are the in-built command prompt and Launch4j. The only requirement is patience.

For this case scenario, I have created a simple GUI in JAVA which display a pop-up box with “Hello World” written in it. Let’s start!

Create Java file/ Locate Java file

Step 1: First of all, Locate your java file you would like to convert to an executable file.

Locating The Java File

Step 2: Open command prompt (windows key + r, type “cmd”, press “ok”)

Open Command Prompt

Step 3: Locate to the folder you have your java file using “cd” command.

Using "cd" command

Step 4: Let’s compile your java file to create a class file. Type “javac yourfilename.java”.

Compile Java File

Check if your file is working with “java yourfilename.java”.

Check Java File

Convert JAVA file to JAR file 

Step 5: Secondly, lets convert your whole file to a JAR file. JAR file is a java archive file that contains all your data and resources used in your java file as a single archive file. We need this to bundle all our files to make them executable. 

Type “jar cvf yourfilename.jar yourfilename.class”.

JAVA to JAR

Editing MANIFEST.MF from JAR

Step 6: Thirdly, try running your jar file using “java -jar yourfilename.jar”. You will encounter an error “no main manifest attribute, in yourfilename.jar”. This is caused because the jar file we created creates a default manifest.mf file which does not know where your main method is located.

(Check this by opening jar file as ZIP. It is located inside “META-INF/MANIFEST.MF”)

Checking JAR

Step 7: Now we need to add the main method located class to the manifest file from outside of the jar file.

Locate main method

Step 8: Let’s create a simple text file named “manifest.txt”. Add “Main-Class: yourfilename”, “yourfilename.java” must contain the main method which executes your whole program. Make sure you enter an extra line of empty space at the end. Save the file.

JAVA to JAR

***Do not try to add this manually to the MANIFEST file inside JAR file, It will corrupt it.***

Step 9: Go back to command prompt and type “jar cmf manifest.txt yourfilename.jar yourfilename.class”. This command will add the main method located class inside “MANIFEST.MF” file.

Modifying Manifest File

Step 10: Let’s check if the JAR file runs now. It works!

Run JAR file

Convert JAR file to EXE file using Launch4j 3.5

Step 11: Finally, we can convert to executable by converting our JAR file to EXE file. For this, we require you to download the application Launch4j here.

***Reminder: Please install the 3.5 version, another version may cause error because it might require you to sign the EXE file***

Launch the app.

Launch4j App

JAR to EXE: How to Use Launch4j? 

Step 12

  • Choose where you would like to place your EXE file by naming it in “Output file:”
  • Select the Jar file we created from “Jar”.
  • Use “Wrapper manifest:” for UAC(User Account Control)
  • “Icon:” is used to select your desired icon for the EXE file
  • Hold your mouse for further queries of other functons in textfields

Launch4j Details

Step 13: We need to define JRE minimum version for EXE to be compatible with. Check your JRE(Java Runtime Environment) by typing “java -version” in command prompt. Select your java version or earlier.

JRE Version

Step 14: Click the floppy disk icon in the top left to save the .xml file anywhere you like. 

Saving File

Step 15: Then click the settings icon in top left to create the EXE file, which should give you a similar message like this:

Compiling File

Step 16: Locate the EXE file where you saved it, and run it! JAVA to EXE is done!

Launching EXE

Conclusion

In conclusion, the complete guide to convert a JAVA file to an executable file is fairly easy but requires no mistakes. Now, you do not need the JAVA environment to run your file.  If you encounter any errors and/or have suggestions, please leave a comment below! We will get back to you as soon as possible!

Read our other articles:

Comments (9)

  1. Great article!!!

    Important tip (at least for me): when create manifest.txt, don’t forget to add new line at the mentioned text.

    See https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html: An existing manifest file must end with a new line character. jar does not parse the last line of a manifest file if it does not end with a new line character.

  2. Iam using lunch4j and bundled the jre 1.8 version. And it takes 2-3minutes to start the application . Could you please help me find the reason

    • I would suggest to rebundle in a different JRE version and see if there are any changes. As for the reason, we would first need to see if the problem is from launch4j or JRE. To test it, try to run the JAR file and if the load time is the same on JAR file then the problem lies within JAVA rather than launch4j. Do tell us the result after you test!

  3. My program is a little more complex than “Hello World” It uses the Scanner class but outside of that it’s not complex. It doesn’t start when I click it, but it shows no errors. Clicking the play button in Launch4j just freezes it. What could I be missing? It runs fine in Command Prompt

    • Do you have a GUI to run the program on like swing? If not, im assuming you are trying to run your code through cmd window. If so then, you need to manually add code in your java file to execute cmd.exe using Runtime and Process classes which will pop open your your executable in cmd. If you do have a GUI setup, then make sure your manifest file is configured properly like shown above.

  4. i follow exactly the “HELLO” program.
    Generated EXE show error “A JNI error has occurred, please check your installation and try again “

    • Hello there! This type of error occurs when you have two different java JDK and/or JRE versions installed. You have probably compiled the file in the latest version and tried to run in a different previously installed older version. To fix this error, try to uninstall the previous version so that you have only one JDK to run the program from.

      Regards,
      Technoloxy

  5. Omg. Only one that works. Thank you soooo much!

Comment here