UPDATE (26 Aug 2009): NetBeans IDE can create executable JAR for any Java project. The steps described in this post are just one way of creating the executable JAR and not the only way. Following the steps described in this post any Java project can be bundled as a JAR executable project.
Topics covered in this post:
Topics covered in this post:
- How to create a desktop application in NetBeans?
- Where is the JAR file after building the application in NetBeans?
- Can a Java Desktop application started by double clicking?
We will start with creating a New Project in NetBeans. This is done by choosing menu File > New Project...
In the New Project dialog box choose Java in categories and Java Desktop Application in the projects List box. Then click the Next button.
Choose all the default values offered by NetBeans IDE in the New Desktop Application dialog as we want to see what is offered as defaults by NetBeans. Click Finish to allow NetBeans to create a new project for us.
After we click finish we get a desktop application with some boilerplate code and windows.
Click the Run button in the tool bar or choose menu Run > Run Main Project to see what is displayed when we Run the application with the default code.
We can see that the generated project is a complete application which runs and shows a window with menu and status bar. To run the application outside NetBeans let us build the application. The current version of NetBeans IDE 6.7.1 compiles and builds the project automatically as you save the project. Let us invoke the menu Run > Clean and Build Main Project to see the build process and the message which is shown after a successful build. This message has a tip for us.
The build successful messages tells us the exact location where the application JAR file is kept. We can navigate to that folder through Explorer and double click the JAR file.
The application is not dependant on NetBeans once it is build. Check the JAR file in the folder, in our case it is DesktopApplication1.jar.
When we double click this JAR file the application starts as if like a EXE file. Java is registered in windows registry as the handler of JAR files so when we double click the JAR file windows shell invokes Java Virtual Machine and hands this JAR file name over to it, so it can execute it for us.
Sometimes software like Nokia PC Suite adds JAR file handler entries in windows registry, in that case the JAR file may not run exactly as I have shown. If that happens just right click on the JAR file and choose Open With option. You will get all the application names registered to handle JAR files and choose Java in this menu to achieve the execution of JAR file.
The JAR file created by NetBeans is executable because NetBeans adds a MANIFEST.MF file in the JAR with proper entry of the Main class of the project.
with regards
Tushar Joshi, Nagpur