Making a Java executable jar in Netbeans
From REM
Beginners Guide: How to Make a Stand-Alone Java Application with SerialForwarder using an Executable Jar in NetBeans 5.0 (Beta Version 2).
A pdf version of this how-to is also available.
Making an executable jar from NetBeans is rather straight forward, until you need to add any sort of third party files or jars. A quasi-helpful FAQ that is geared towards GUI’s can be found on the NetBeans GUI page.
You’ll need to do a few things to get you jar working.
Step 1: jar your tinyos java tools. For reference I’ve called my “sf.jar”.
Step 2: add the required compile and run-time JARs and class Folders. First, right-click your project to get the “project properties” screen, shown below. Use the “Add JAR/Folder” button to point to the correct resources. In the screen shot below, you’ll notice I’ve added my sf.jar file and the swing-layout.jar file (this is used for the gui, see the netbeans FAQ link above). ***Be sure to add these to both the “compile” and “run” options as shown by the tabs to the left of “compile tests” and “run test”.
Ok, now that you’ve added the correct JARs and Folders – DON”T close this window yet, we’ll do more in the next step.
Step 3: make sure your “Java Platform” jdk is identical to the tinyos installed version. This was a big source of error for me since my NetBeans installation uses jdk1.5 but the tinyos install uses jdk1.4. From the “project properties” window we were just in Step 2, click on the “Manage Platform” button – we’re going to specify that NetBeans uses the TinyOS-included jdk 1.4. You should get a similar to below – but yours should not have the “Java HotSpot” – this is what we’ll add.
Now, click on the “Add Platform” button. You should see a screen similar to the one below pop-up. You’ll need to navigate your directories to the jdk in the tinyos installation. Notice that my installation places the jdk in “C:\tinyos\” and the actual jdk is “jdk1.4.1_02.” Click “Finish” to add this to your Java Platforms. Ok, now your done with the project properties.
Step 4: modify your “build.xml” file. The “build.xml” file is automatically generated by NetBeans – look in your files, it should be next the “manisfest.mf” file. This is the trick part – this will include additional files into the executable jar.
Essentially you need to add these lines to include the “sf.jar” file.
<jar update="true" destfile="${dist.jar}">
<zipfileset src="c:/MyJava/sf.jar"/>
</jar>
Now, your “build.xml” should look like this. Notice that I’ve added the lines to include the swing-layout.jar for the gui as well – this syntax is taken directly from the very first link in this document to the NetBeans FAQ.
<?xml version="1.0" encoding="UTF-8"?>
<project name="NBgui" default="default" basedir=".">
<description>Builds, tests, and runs the project NBgui.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-post-jar">
<jar update="true" destfile="${dist.jar}">
<zipfileset src="${libs.swing-layout.classpath}"/>
</jar>
<jar update="true" destfile="${dist.jar}">
<zipfileset src="c:/MyJava/sf.jar"/>
</jar>
</target>
</project>
Step 5: Clean and compile your project. NetBeans will automatically generate an executable jar file. NetBeans will (by default) place this jar into the ~/dist folder in the project.
Step 6: Double click the jar – unless your Windows ( or Mac ) jdk is jdk1.4, this will not work. Instead, open cygwin and navigate to the ~/dist folder. Launch the application by executing the command line “jar –jar YouJarName.jar”.
