java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.lang arrow Make a JAR executable
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Make a JAR executable E-mail
User Rating: / 7
PoorBest 

In the manifest file of a JAR, it is possible to specify the class to be used when the JVM is lauched with the JAR as parameter. The class must have a main().

Try with this simple class:

import java.awt.*;
import java.awt.event.*;

public class MyClass {
 public static void main(String[] args) {
    Frame f = new Frame();
    f.addWindowListener
          (new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                 System.exit(0);
                 }
              }
    );
    f.add(new Label("Hello world"));
    f.setSize(200,200);
    f.setVisible(true);
   }
}

Then create this manifest file (manifest.mft) with any text editor.

   Manifest-Version: 1.0
   Main-Class: MyClass
   Classpath: .\MyJar.jar

Next, you include the manifest file in the JAR (MyJar.jar) with the MyClass class.

   jar cvfm MyJar.jar manifest.mft MyClass.class

Then you are able to start the MyClass.class by double-clicking on the MyJar.jar file (if the JRE is correctly installed) or by typing

   java -jar MyJar.jar

On NT, you can also make JARs run from the command-line by setting the PATHEXT environment variable, for example

   set PATHEXT=.EXE;.BAT;.CMD;.JAR

Then if you have the jar file MyJar.jar accessible via the PATH environment variable, typing "MyJar" on the DOS command line will invoke "java -jar MyJar.jar" .


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.