Having tested our code in Eclipse and proved it works there we now need export our code to the file system.
To get our code out of Eclipse we will export the files from Eclipse to our file system.
- From the menu bar select ‘File-Export’ which opens the export dialog box.
- Expand the Java folder and highlight the ‘Runnable JAR file’ option.
- Press the ‘Next’ button
- Select the launch configuration for the project
- Select an export destination including the JAR file name. I have kept this the same as the Java Project to make it easier to identify things later.
- Select the radio button ‘Package required libraries into generated JAR’
- Press the ‘Finish’ button to write the JAR file to the location you specified.
So now we have exported our Java Archive (JAR) file we now need to prepare it for wrapping up into an EXE file.
- Open WinRAR
- From the menu bar select ‘File-Open archive’ and navigate to where the JAR file was placed. You made need to select ‘All files’ from the list of file types
- This then presents a list of files and folders contained within the JAR file. Now we are interested in the META-INF folder as this is where we will find the MANIFEST.MF file.
- Double click on the META-INF folder to display files and folders located there. You should find that you only have a link to the parent folder and the MANIFEST.MF file.
- Double click the MAIFEST.MF file to open the file. The file should contain the following:
For our simple test application we do not need to alter the manifest but now we know we can when the time comes.
The JAR file now needs to be wrapped up into executable code that allows a user to launch like any other application. For this we will use Launch4J. Locate where you installed this software and find the file ‘launch4j.exe’.
- Double click the file to launch.
- Provide a path and file name including extension that you want to call your final executable program. In my case I have gone with ‘T:\Personal\java\Build\BuildApp1\BuildAnApp1.exe’
- Next select the JAR file we created earlier.
- Move to the tab called ‘Header’ and check the radio button ‘Console’ for the ‘Header type’. For all the other examples we will not need to do this as they will be GUI applications but this first application is a basic console application.
- Next move to the ‘JRE’ tab. In order to use this tool you must specify the minimum Java Runtime Environment that the application can use. I tend to use ‘1.6.0’ for this.
- Finally move to the Messages tab and check the ‘Add custom messages’. This is not really required but may be useful if you have trouble getting things working.
- Save the configuration by providing a path and file name. I save the configurations with the JAR and EXE files so I know they all go together.
- The last thing to do is build the wrapper which is achieved by pressing the button that looks like a gear. The log box at the bottom is used to provide progress information.
All that remains is to test our new EXE file. Open a Command Prompt window and navigate to the location of the EXE file and simply type in the filename of the EXE and press return. You should see displayed on the screen ‘The start of our application.’
That’s it, we can now move on to build our app knowing that the basic building blocks all work. Let’s go build a GUI.