Testing our Environment – part 5

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.
Figure 8 Select export type
Figure 8 Select export type
  • 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.
Figure 9 Runnable JAR settings
Figure 9 Runnable JAR settings

click here to download a copy of the JAR file

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
Figure 10 Select JAR file
Figure 10 Select JAR file
  • 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.
Figure 11 Contents of JAR file
Figure 11 Contents of JAR 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:

Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: softwarepulse.app.BuildAnApp1
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

click here to download a copy of the Manifest.mf file

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.
Figure 12 Launch4J Basic tab
Figure 12 Launch4J Basic tab
  • 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.
Figure 13 Launch4J build process log
Figure 13 Launch4J build process log

click here to download a copy of the executable code

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

Figure 14 Running the first application
Figure 14 Running the first 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.

 

You can download the complete source for this example here (zip file 25 kb)

 

Advertisements
Testing our Environment – part 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s