JavaRush /Java Blog /Random EN /Convert JAR to executable (.exe) file
Zheleznyak Maxim
Level 47
Moscow

Convert JAR to executable (.exe) file

Published in the Random EN group
The shortest instructions. We are writing a simple test application, say "Hi Amigo!" to the HiAmigo.txt file 100 times.
public class Main {
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\temp\\HiAmigo.txt");
        FileWriter fileWriter = new FileWriter(file);

        for (int i = 0; i < 100 ; i++) {
            fileWriter.write("Hi Amigo! \n");
        }
        fileWriter.close();
      }
}
We check that everything works at this stage (this is important!) Converting JAR into an executable (.exe) file - 1 Go to File -> Project Structure -> Artifacts -> + JAR -> From Modules with dependencies.. Converting JAR into an executable (.exe) file - 2 Specify the path to our Main.class: Convert JAR to executable (.exe) file - 3 Click OK Click Build Artifacts - > Action - > Build Converting JAR into an executable (.exe) file - 4 Our artifact appears: Convert JAR to executable (.exe) file - 5 We delete our test file “C:\\temp\\HiAmigo.txt” and run the JAR. The file should appear again. If it appears, everything is ok. Let's move on. Download the tool from the site https://launch4j.sourceforge.net/ (I recommend version 3.14, I’ll explain why later). Install and launch. We need two fields. We indicate our JAR and where to put the finished .exe. Converting JAR into an executable (.exe) file - 6 Now comes the most interesting part. Imagine a situation where the machine where we plan to use our application does not have the JRE installed and there is no way to install it. This often happens for security reasons. In addition, it would be nice to provide our application with a higher level of autonomy. Let's do the following. Let's create a separate directory for our application, let's say OurApp. Inside we create a JRE folder. Download the JRE (in my example, let it be jre-8u361-windows-i586), install it somewhere (if it’s a ready-made archive, unzip it) and extract all the files from it. We copy everything into \OurApp\JRE\ it should look something like this: Converting JAR into an executable (.exe) file - 7 In Launch4j, go to the JRE tab and in the Bundled JRE paths field: specify the name of the JRE directory Converting JAR into an executable (.exe) file - 8 Click on the gear and select any file to save the configuration like file.xml Converting JAR into an executable (.exe) file - 9 After clicking on save, ours will appear exe. Converting JAR into an executable (.exe) file - 10 Copy the exe and place it next to the JRE folder. Converting JAR into an executable (.exe) file - 11 Delete the test C:\temp\HiAmigo.txt. We launch the exe and watch how a new one is created. That's all. Now the OurApp directory can be archived and transferred anywhere, the exe will work. Important: The JRE version in the folder must be no lower than the one on which the artifact was built. Check the field: Converting JAR into an executable (.exe) file - 12 In my case, there were compatibility problems with the x86 platform and I used wrapper version 3.14, since it allows you to explicitly specify the type of target architecture. In general, thank you all for your attention. This was my first instructional article, please do not judge strictly. Peace for everyone!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION