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

37 comments:

  1. wow, as a beginner as I am, this is streight forward!
    This should bi in Java doc as it is...
    Thank you!!!

    ReplyDelete
  2. Isn't this a bit risky since creating a desktop project using NetBeans means that you are using JSR 296 (Swing Application Framework), and the fate of that is quite sad at this moment?

    ReplyDelete
  3. Rohan, the Swing Application Framework is still the fastest way to develop, and has a solid code base. It works, is mature and leaves little to desire. So it merits this excellent introductory overview. Which is to-the-point.

    ReplyDelete
  4. @joop eggen: I disagree. SAF is not ready (that's why it is not in JDK7), has hardly any backing from Sun since Hans Muller left. Heck, it might merit a to-the-point overview, but novices reading this entry will barrel head long into developing with SAF and might later smash their heads seeing no progress on the SAF support. Anyway, another reason I don't understand why SAF needs a mention in this blog post is because even normal JARs can have manifests with main class specified so that they can be launched by double clicking on them. And a desktop application needn't be based on SAF.

    ReplyDelete
  5. How do I include database with the jar file ? and can anyone help me with transferring the database from one system to another ? using netbeans preferably..

    ReplyDelete
  6. @Rohan,

    Let me clarify one point here, this post was not intended to be a post supporting what you call Swing Application Framework or JSR296. I just picked one use case from the existing NetBeans IDE 6.7.1 installation and elaborated it here.

    After reading your comment I started thinking about the SAF and JSR296 and that the originator Hans Muller is now not with Sun anymore. My question is if this is such a obsolete system why is it still maintained in production version of NetBeans IDE 6.7.1?

    I agree that making executable JAR is a common feature in NetBeans and this post was just to highlight that feature, incidentally I picked the project option which looked similar to a RCP application as it was easy to demonstrate.

    with regards
    Tushar

    ReplyDelete
  7. @Tushar: Thanks for your explanation. I completely agree with you that your post was not about SAF and I just wanted to point out that people might think that to make JARs launch through double click, you have to create a SAF based project in NetBeans. SAF is not obsolote, but whether it is evolving into a final and supported framework is the question worth answering.

    AFAIK, Netbeans even supports beans binding though it has been kindof in the abandoned mode as SAF has been.

    ReplyDelete
  8. In database appliation i can't able to run jar file.Please clarify this point.

    ReplyDelete
  9. SAF is in a VERY sad state indeed.http:// weblogs.java.net/blog/alexfromsun makes that very clear.

    ReplyDelete
  10. @Shashank

    There can be many reasons why you could not run the JAR file. Please ask your question on nbusers@netbeans.org mailing list so many people can think on it and may reply you.

    When I tested with NetBeans IDE 6.7.1 and JavaDB application, I was able to run the JAR file as an executable.

    Please explain your environment and provide any error message you are getting on the mailing list.

    with regards
    Tushar Joshi, Nagpur

    ReplyDelete
  11. This is not executable file. You may use Jsmooth for making an exe file.

    ReplyDelete
  12. What about the standalone DB application, how we can enable Java DB while executing JAR

    ReplyDelete
  13. i am a beginner. i have this type of requirement:
    in netbeans:-
    how to fetch image?
    how to use command buttons and code to jump from one form to another by click event of it?

    please kindly answer immedietly it's urgent:
    send it on my yahoo-id jayraithatha@yahoo.com

    ReplyDelete
  14. Thanks for ur information.....

    ReplyDelete
  15. whatever i know..! is it possible to create a java application as .exe which can be installed in a system and then run as like all other software's.just like netbeans IDE...install it in a system and run it...????

    ReplyDelete
  16. @dhanabalan,
    The answer to your quenstion is yes, and the details are available in the comments above.

    Alternatovely you can also use installer project like IzPack to create an installer for your Java product.

    with regards
    Tushar

    ReplyDelete
  17. thanks for Ur help... is there anything i can know about IzPack from u...

    ReplyDelete
  18. http://java-source.net/open-source/installer-generators/izpack

    ReplyDelete
  19. The help provided above really worked and it helped me a lot. After all it made me the first student who can run his java netbeans programs on other computers
    Thank you
    Anubhav

    ReplyDelete
  20. can anyone tell how to link different projects/applications in netbeans through jbuttons???

    ReplyDelete
  21. I am a beginner to desktop app and wish to build a desktop application. Please suggest latest technology to be followed?
    Thanks in advance!!

    ReplyDelete
  22. Pallavi,

    Using NetBeans Platform for your Desktop Application may be a good choice if the application is more than a practice project. It involves initial learning curve but it is worth learning.

    For practice desktop projects just using Swing may do.


    with regards
    Tushar

    ReplyDelete
  23. i want to know that how to open report in PDF format on button click

    ReplyDelete
  24. my jar file is not running...can any1 tell whats the problem...& how can i resolve this problem

    ReplyDelete
  25. how to make jar files handler and which software is to use

    ReplyDelete
  26. great tutorial,I didn't know its that easy in netbeans. by the way I have also shared some tips around how to create executable jar in java and how to view contents of jar file. you may find interesting.

    ReplyDelete
  27. My JavaApplication uses JMF(java media Framework), it is running currectly in NetBeans. I have created jar of that when I double clicks on the jar file nothing happens. I tried to run it from cmd it shows exception, Exception is generated for some one jmf library, what to do to solve it?

    ReplyDelete
  28. yahooooooooo... its work for me!!!

    but how i can make .exe file of java program which is not desktop application??

    ReplyDelete
  29. Thanks, this is the last step I need, but I have one question, please
    If I want to move this file to other computers, should I move it alone, with the whole fine "dist" or I have to compres them together in a software? thx

    ReplyDelete
  30. by double clicking on jar file, my jar file is not running by getting a pop up window that says that "windows cannot open this file"..:(
    i am using netbeans7.0 beta version.
    please provide me solution .:(

    ReplyDelete
  31. my jar file is not running and i am getting an error that" windows cannot open this file"..:(
    I am using netbeans 7.0 beta version. Please give me some solution of it...:(
    thanks in advance..

    ReplyDelete
  32. Any body can give me a sample desktop application for learn how to develop desktop application (with MDI Parent Form).

    ReplyDelete
  33. thanks lot...thank u so much..but i have to design that window as like getting input from user and display the output..for that which place have to edit..in desktopApplication1.java or in source part of that design???

    ReplyDelete
  34. i m developing desktop app. n having problem in finding browse button. is it there or not? if not then wats the other way to browse a file

    ReplyDelete
  35. But the jar files didn't run on the computer in which Java Netbeans is not installed. Please clarify this point.

    ReplyDelete
  36. How could i make a desktop application, which include my DLL [written in C] and calls it and pass parameter, just as a standalone Apps do and publish the result from DLL in my apps?Please help me out...am just a beginner and searching for a suitable impressive ,easy to learn UI for my dll. Is NetBeans suitable or Visual Stdio?

    ReplyDelete