Wednesday, April 25, 2012

How to execute your jar file by double clicking on it ?

Any jar package for executing will need a file named MANIFEST.MF which located in META-INF directory in that package. In that file you can tell VM where to find main method of your program to start execution.netbeans by default will generate an executable jar file (I mean JVM executable package and not binary exe file!) when you build your project as Desktop Application.If  you copy only that jar file out side of build directory then this application will not work then the error like could not find main class the program will exit.we can make our normal jar files as executable jars just by add proper manifest file with main class some thing like below.

   Suppose if  you are working with DB application you can specify database driver jar in manifest class-path attribute.


 
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.x.x 
Created-By: 1.x.x (Sun Microsystems Inc.)
Class-Path: mysql-connector-java-5.0.6-bin.jar    
X-COMMENT: Main-Class will be added automatically by build 
Main-Class: path.to.main.class

No comments:

Post a Comment