No. 200 on May 10 Author: Shawn Silverman Translation: Sean
Abstract This article describes how to turn an unforgable Java Archive (JAR) file to execute without having to manifest file directly. You will learn to write a short program, and double-click the mouse with a double-click mouse in an operating system like Windows or on the operating system like Windows.
You can easily pack a set of Class files and resource files in the app to a JAR. In fact this is a purpose of the JAR file. Another purpose is to allow users to easily execute applications that are packaged into the JAR file. Then why JAR files only use the file to occupy a secondary status throughout Java, and local execution is ignored?
To perform a JAR file, you can use the -jar option of the java command. For example, if you have a file called myjar.jar. This JAR can run, you can run it: java -jar myjar.jar. Another way is that when Java Runtime Environment (jre) has been installed on an operating system like Windows, associate the JAR file with the JVM ( Associated java.exe with jar files) You can run this application by double-click JAR. Of course, the JAR file must be executable. The current question is: How to make a Jar that can be executed?
The Manifest file and the main-class entry are in most JARs, and a file called Manifest.mf is saved in a directory called Meta-INF. That file contains a special table name name called Main-Class, telling the java -jar command which class should be executed.
The problem is that you must add an appropriate entry to the Manifest file, and must be in a certain location and in a certain format. Unfortunately, not everyone likes to open a write panel editing profile.
Let the API help you complete the task since Java 1.2 release, a java.uil.jar package, so that you can easily handle the JAR file. (Note: This package is based on java.util.zip) Special, the JAR package allows you to operate those manifest files easily through the Mainfest class.
Let us write a program with this API. First, this program must know three things: 1. We want to make it run. JAR file. 2. Run JAR's primary class (this class must be included in JAR). 3. Output the file name of the new JAR file, because we cannot easily cover the original file.
The three points required by writing the program will form the parameters of our program. Now let's choose a proper name for this program. What is Makejarrunnable sounds?
Checking parameters for the main method assumes that our MAIN method entry point is a standard main (String []) method. We should check the parameters of the program: if (args.length! = 3) {system.out.println ("Usage: makejarrunnable"
Access jar and the jar manifest file first, we have to create some objects to understand and manifest the jar: // Create the JarInputStream object, and get its manifest JarInputStream jarIn = new JarInputStream (new FileInputStream (args [0])); Manifest manifest = jarin.getmanifest (); if (manifest == null) {// this will happen if no manifest existsmanifest = new manifest ();}
Set the main-class attribute We put the main-class entry to the main properties section of the Manifest file. Once this property is obtained from the Manifest object, you can set the required main classes. However, what is the original JAR in the main-class attribute already exists? Here we only simply output a warning and then exit. We can join a command line parameter telling the program to use the new value, instead of the old one: attributes a = manifest.getMainAttributes (); string oldmainclass = a.putValue ("main-class", args [1]);
// if an old value exists, tell the user and exit
IF (OldMainClass! = null) {system.out.println ("Warning: Old main-class value is:" OldMainClass); System.exit (1);
Output new JAR we need to create a new JAR file, so we must use the JaroutputStream class. Note: We must determine that we don't need to use the same name as the input file as the name of the output file. There is also a solution that the program should take into account that both JAR files are the same, so that users have covered the original file, if he is willing to do this. However, I have retained this, as an exercise of the reader. From the following code: System.out.Println ("Writing to" args [2] "..."); jaroutputstream jarout = new naroutputstream (new fileoutputstream (new fileoutputstream (Args [2]), manifest); we must be entered JAR writes each entry to the output JAR, so iterations each entry: // Create a Read Buffer to Transfer Data from the Input
BYTE [] BUF = New byte [4096];
// iperate the entry
Jarentry Entry; while ((entry = jarin.getnextjarentry ())! = Null) {// Exclude the manifest file from the old jar
IF ("meta-inf / manifest.mf" .Equals (entry.getname ())) Continue; // Write the entry to the Output Jar
Jarout.putNextentry (entry); int = jarin.read (buf))! = -1) {Jarout.write (buf, 0, read);}
Jarout.closeentry ();
// Flush and close all the streams
Jarout.flush (); jarout.close ();
Jarin.close ();
The completion procedure is of course, we must put these code in a class MAIN method and require a lot of Import code. Complete program: http://www.javaworld.com/javaworld/javatips/javatip127/makejarrunnable.zip
The program use example allows us to apply this program to an example. Suppose you have an app, the entry point of the program is a class called HellorunnableWorld, and then you have created a JAR called myjar.jar, which contains the entire program. Run Makejarrunnable: Java makejarrunnable myjar.jar HellorunnableWorld myjar_r.jar is as mentioned earlier, pay attention to my parameter order. If you have forgotten the order, there is no parameter to run a program, which will respond to a usage prompt information.
Try running the java -jar command to myjar.jar. Then to myjar_r.jar. Pay attention to differences! Ok, you have done all this, browse each JAR's Manifest file (Meta-INF / MANIFEST.MF)