How to Convert a Java Program to an .exe File

To convert a Java program to an .exe file, first, you need to convert it into a .jar file. After this, you can use various tools to convert it into an .exe file.

In this blog, we are going to understand how you can convert a Java program to a JAR and then to an .exe file with a detailed explanation and steps.

Table of Contents:

What is an .exe file?

An .exe is a file format specific to Microsoft Windows for running programs directly. It is a program stored in a binary form that could be loaded and run by the operating system itself. Double-clicking on an .exe file makes it easy for end-users to run a program without the installation of Java or the command line.

What is a JAR file?

A (JAR) Java Archive file is a compressed package that contains Java class files, resources such as images and configuration files, and metadata in a single file. It is based on the format of a ZIP file and has the .jar extension. These files reduce the size of the application and are highly secure.

The type of executable file that is produced when you convert your Java code depends on the platform that you are using. For example, in Windows, it is .exe while in Mac OS, it is .app. On Linux, it can be an RPM package or a shell script.

Note: Java programs are platform-independent and run typically using the Java Virtual Machine (JVM). But .class or .jar files are not directly run by Windows like an .exe file is run. To run a Java program as a natively run Windows application, we transform it into a JAR file, and from there, that JAR is converted into an EXE.

1. Convert Java Program to JAR

You can create a JAR file using different methods and tools. Some of them are as follows:

a. Convert a Java Program to a JAR file using the Eclipse IDE

You should follow these steps to convert a Java program to a JAR file.

Step 1: Ensure Your Java Project is Ready

Load your Java project in Eclipse and make sure your project compiles without giving any errors.

Step 2: Configure the Entry Point (if needed)

If your program has a main method, you need to define the entry point for the execution of the program.

  • Right-click on your project -> Properties.
Convert a Java program to a JAR file using the Eclipse IDE-1
  • Navigate to Run/Debug Settings -> Click New -> Choose Java Application.
  • Select the main class and click OK.
Convert a Java program to a JAR file using the Eclipse IDE-2

Step 3: Export as a JAR File

  • Right-click on your project in the Package Explorer.
  • Select Export -> Expand Java -> Choose Runnable JAR file -> Click Next.
Convert a Java program to a JAR file using the Eclipse IDE-3
  • Select the files you want to include (usually, the src or bin folder).
  • Choose the export destination (e.g., C:your_projectyourfile.jar).
  • Click Next, then Next again (skip optional options like source file inclusion).

Step 4: Create a Manifest File (for Executable JAR)

If your program has a main method and you want an executable JAR:

  • On the JAR Export wizard, check Generate the manifest file.
  • Select the main class (browse and choose the correct class containing public static void main(String[] args)).
  • Click Finish.
Convert a Java program to a JAR file using the Eclipse IDE-4

Step 5: Run the JAR File

  • Open the terminal (Command Prompt or Terminal).
  • Navigate to the directory containing the JAR file.
  • Run the command:
java -jar yourfile.jar
Convert a Java program to a JAR file using the Eclipse IDE-5

Source code of the Main.java:

Java
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

b. Convert a Java program to a JAR file using NetBeans IDE

If you’re using NetBeans, you can easily create a JAR file for your Java application by following these steps:

Step 1: Open Your Project in NetBeans

  • Launch NetBeans and open your Java project.
  • Make sure your project includes a class with a main method.

Step 2: Set the Main Class

  • Right-click on your project in the Projects window.
  • Select Properties.
Convert a Java program to a JAR file using NetBeans IDE-1
  • In the Project Properties window, click on Run.
  • In the Main Class field, select or enter the class containing public static void main(String[] args).
  • Click OK.

Step 3: Build the Project

  • Click Run -> Clean and Build Project (or press Shift + F11).
Convert a Java program to a JAR file using NetBeans IDE-2
  • NetBeans will compile your project and generate a JAR file automatically.

Step 4: Locate the JAR File

  • Go to your project folder on your computer.
  • Go to dist/ inside your project folder.
  • You should see your JAR file (e.g., MyProject.jar).
Convert a Java program to a JAR file using NetBeans IDE-3

Step 5: Run the JAR File

To test if the JAR file works, open the Command Prompt and go to the dist folder

Then run the following command:

java -jar MyProject.jar
Convert a Java program to a JAR file using NetBeans IDE-4

Note: If your project is using the external libraries, NetBeans will automatically include them in the lib/ folder inside dist/. To run the JAR properly, make sure all required libraries are present in that folder.

c. Convert a Java program to a JAR file using a Command Prompt (CMD)

Step 1: Write Your Java Program

Open Notepad and type your Java code.

Example:

Java

Save it as My.java.

Step 2: Compile the Java File

Open the Command Prompt (CMD). Go to the folder where your Java file is saved.

Compile the Java program using:

javac My.java

It will generate a My.class file in the folder.

Step 3: Create a Manifest File

To specify the main class, you need to create a manifest file.

Open Notepad and type:

Main-Class: My 

Save it as a MANIFEST.MF in the same folder.

Note: Make sure there is an empty line at the end of the file.

Step 4: Create the JAR File

Run the following command in cmd:

jar cfm My.jar MANIFEST.MF My.class 

where,

  • c: Create a new JAR file
  • f: Specify the filename
  • m: Include a manifest file
  • My.jar: Output JAR filename
  • MANIFEST.MF: Manifest file for specifying the main class
  • My.class: Compiled Java file

 Step 5: Run the JAR File

To test the newly created JAR file, use:

java -jar MyProgram.jar

It will give the output as follows:

Convert a Java program to a JAR file using a Command Prompt (CMD)
Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

2. Convert JAR to EXE

a. Convert a JAR file to .exe using Launch4J

Launch4J is a popular tool for converting a Java JAR file into a Windows executable (.exe). Follow these steps to convert your Java program into an executable file.

Step 1: Create a JAR File

Before using Launch4J, you need to have a JAR file of your Java application. If you don’t have one, create one.

Step 2: Download and Install Launch4J.

  • Go to the Launch4 Jofficial website.
  • Download and install Launch4J on your computer.

Step 3: Open Launch4J

In the Basic tab:

  • Click on Browse and select your .jar file.
  • Choose the Output file name, e.g., Intellipaat.exe.
  • Set the Icon File if you want an icon.
Convert a JAR file to .exe using Launch4J-1

1. In the JRE tab:

  • Set the Minimum JRE version (e.g., 1.8 for Java 8 or later).
  • You can also select a Bundled JRE Path if you want to include Java in it.
Convert a JAR file to .exe using Launch4J-2

2. In the Splash tab:

  • You can add a splash screen to show while your program loads.

3. Click on Save Configuration to keep settings for future use.

Convert a JAR file to .exe using Launch4J-3

Step 4: Generate the Executable (.exe)

  • Click on Build Wrapper.
  • Launch4J will create an EXE file in the chosen output directory.
Convert a JAR file to .exe using Launch4J-4

Step 5: Test Your Executable

  • Go to the folder where the .exe file is saved.
  • Double-click on the .exe to run it.

b. Convert a JAR file to .exe Using JSmooth

JSmooth is a tool that converts Java JAR files into Windows executables (.exe), allowing users to run your program without needing to install Java separately. Follow these steps to create an .exe file using JSmooth.

Step 1: Create a JAR File (if not already done)

You can use Eclipse or NetBeans, as shown above, to create a JAR file.

Step 2: Download and Install JSmooth

  • Go to the JSmooth official website.
  • Download and install JSmooth on your computer.

Step 3: Configure JSmooth to Create an .exe File

1. Open JSmooth.

2. Click New to create a new project.

3. In the Skeleton tab, choose Windowed Wrapper (for GUI apps) or Console Wrapper (for terminal-based apps).

4. In the Executable Tab, set:

  • Executable Name: Choose the name for your .exe file (e.g., My.exe).
  • Jar File: Click Browse and select your My.jar.
  • Main Class: Enter the main class name (My).

5. In the JVM Selection tab, specify the minimum Java version required (e.g., 1.8 for Java 8).

6. Click Save, then Compile to generate the .exe file.

Convert a JAR file to .exe Using JSmooth

Step 4: Test the .exe File

  • Go to the folder where the .exe was generated.
  • Double-click the .exe file to run it.

c. Convert a JAR file to .exe Using Jar2Exe

Follow these steps to convert your Java program into an executable file.

Step 1: Create a JAR File

Before using Jar2Exe, you need to have a JAR file of your Java application. If you don’t have one, create one.

Step 2: Download and Install Jar2Exe.

  • Go to the Jar2Exe official website.
  • Download and install Jar2Exe on your computer.

Step 3: Select Your JAR File

Convert a JAR file to .exe Using Jar2Exe-1
  • Click Next, and browse to select the JAR file you want to convert.

Step 4: Choose Output Type Between the following:

  • Console application
  • Windows GUI application
  • Windows Service
Convert a JAR file to .exe Using Jar2Exe-2

Step 5: Specify Application Settings:

  • Choose Main Class (Jar2Exe auto-detects it or lets you specify manually).
Convert a JAR file to .exe Using Jar2Exe-3

Step 6: Set Target EXE Path:

  • Specify the name and location for the output .exe file.

Step 7: Build the EXE:

  • Click Finish or Build to generate the .exe file.
Convert-a-JAR-file-to-.exe-Using-Jar2Exe-4

d. Convert a JAR file to .exe Using Maven

By using Maven, you can create an .exe file. The following steps you can follow to create an .exe file.

Step 1: Create a Maven Project

Step 2: Add the Maven Shade Plugin to pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals><goal>shade</goal></goals>
          <configuration>
            <transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.example.Main</mainClass> <!--  your main class -->
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Step 3: Build the JAR File

Click on the Maven icon on the top right, and then run the Maven clean package to build a jar file.

Step 4: Now, to create an .exe file, you can use tools like JSmooth, Launch4J, or Jar2Exe.

Advantages of Converting Java Program to an Executable File

  • It is secure, as the source code is not visible to any of the users.
  • It is easy to run, as there is no need for the command line or any command to run it. You can just double-click on it to run it.
  • They are faster, as they don’t depend on the JVM to run the application.

Disadvantages of Converting Java Program to an Executable File

  • JAR files take up more space as compared to the application.
  • They are platform-dependent. The JAR file must be compiled separately on different platforms.
  • If you change the code, you have to make a new jar file because the implementation of the code will not be passed to the jar file.
  • Debugging is harder as debugging an executed file is harder than debugging an JAR file because it does not have access to the source code.

Conclusion

You can convert a Java program into an .exe file to make it easier to open. First, create a .jar file, then convert it to an .exe using tools like Launch4J or JSmooth. This can help hide the code and let users run the program with a click. However, it only works on Windows and must be updated if the code changes.

To learn more about this topic, check out our Java Course.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Convert a Java Program to an .exe File – FAQs

Q1. How to compile a Java program?

To compile the Java program, we can use the command: javac FileName. java.

Q2. Can we convert Java to .exe?

To convert your JAR file to an .exe file, you can use a tool like Launch4J, Jar2Exe, or JSmooth.

Q3. How do I find my Java exe?

One copy is found in the bin directory of the JRE. The second copy is placed in either C:windowssystem or C:winntsystem32, depending on the system.

Q4. What is a Java executable?

An executable Java class is a class that, when handed over to the JVM, starts its execution at a particular point in the class – the main method, defined in the class.

Q5. Is a JAR file executable?

A jar is an archiving format that not only stores directories and source files but can be run as an executable as well.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner