Sunday, August 23, 2009

Making executable desktop application in NetBeans

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:
  1. How to create a desktop application in NetBeans?
  2. Where is the JAR file after building the application in NetBeans?
  3. 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

Wednesday, August 12, 2009

SHIFT+Click to close all Editor Tabs and ALT+Click to Close all others

Tips covered in this post:

  • How to close all the editor tabs in NetBeans IDE?
  • How to close all other editor tabs except the current one in NetBeans IDE?
  • How to use mouse and keyboard combination for productivity in NetBeans IDE
When there are many editor tabs open in NetBeans IDE and we want to close them all, we have the keyboard shortcut of CTRL+SHIFT+W, but this can also be done using mouse and a single click. Just hold the SHIFT key and click on any of the editor tab close marks and wooooosh all the editor tabs will be closed.

ALT+Click (Option+Click in Mac)
When there are many editor tabs open and we want to keep only one tab open and close all the others in a single click, just hold the ALT button and click on the close button of the editor tab you want to keep open. This will close all the other editor tabs and only the one you clicked is retained.

with regards
Tushar Joshi, Nagpur