Run your JARS with a java -jar command

xiaoxiao2021-03-06  62

Summary

This tip shows how to turn an uncomprogrammed Java file (JAR) to acquireable without having to operate the list file directly. You will learn to develop a short program so that any jar runs with a java -jar command or runs with a double-click operation on a Widnows operating system.

You can easily pack a Java file (JAR) throughout the class and resource. In fact, this is one of the purposes of the JAR file. Another purpose is to allow users to easily perform applications stored in the file. Why did they be implemented as a second type of member in the Java world when they can be implemented as a first class?

To perform a JAR file, you can use the -jar option in the java command. For example, you have a acquired JAR file called myjar.jar, because this file is getting, you can perform it like this: java -jar myjar.jar ..

In other words, when in a Java Run Environment installed on a Microsoft Windows operating system (JRE), you can run the application by double-click the JAR file related JAR file. These JARs must be available.

The problem is: How do you get a JAR?

List files and main-class portals

In most Jars, a file called Manifest.mf is stored in a directory called Meta-INF. In those files, a special entrance called the main-class tells the java -jar command which class will be executed.

The problem is that you have to properly add these dedicated portals to a list file - it must have a specific place and have a specific format. However, some of us don't like editing profiles.

Let the API make these

Since Java 1.2, a package called java.util.jar makes you work with the JAR file. (Note: It is built on a java.util.zip package), and the JAR package makes it easy to operate a dedicated list file via the Manifest class.

Let us write a program with this API. First of all, this program must understand three things:

1. We want to make the JAR that can be acquired

2. We hope to be executed (this class must exist in jar)

3. The name of the new JAR we entered, because we can't easily overwate the file

programming

The above list will form the parameters of our program. Based on this, let's choose a suitable name for this program. How about Makejarrunnable?

Check the parameters of Main

Suppose our primary entry is a standard main (String []) method. We should first check the procedures here:

IF (args.length! = 3) {

System.out.println ("Usage: makejarrunnable"

"

");

System.exit (0);

}

Note how the parameter list is explained, which is important for the following code. The parameter sequence and content are not set here, however, if you want to change their remember to modify other code accordingly.

Access JAR and its manifest file

First, we must create some objects that understand the JAR and inventory files:

// Create a JarInputStream object and get your list

JarinputStream Jarin = New JarinputStream (New FileInputStream (Args [0]));

Manifest manifest = jarin.getmanifest ();

IF (manifest == null) {

// If there is no list, the following code will be executed by manifest = new manifest ();

}

Set the properties of the main-class

We put the main-class entry into the main attribute part of the list file. Once we get this property settings from the inventory object, we can set the appropriate main class. However, what if a main-class property is existing in the original JAR? This program will simply output a warning and exit. Perhaps we can add a command line parameter to tell the program to replace the previous existence with a new value:

Attributes a = manifest.getMainAttributes ();

String OldMainClass = a.putValue ("main-class", args [1]);

// If an old value exists, tell the user and quit

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 ensure that the output and input will not use the same file. In other words, maybe the program should consider the same situation as two JAR files and prompt the user if he wants to overwrite the original file. However, I will reserve this as a reader's practice. Start code!

System.out.println ("Writing to" args [2] "...");

JaroutputStream Jarout = New JaroutputStream (New FileoutputStream (Args [2]),

MANIFEST);

We must write every entry from the input JAR to the output JAR, so reiterate the inlet:

// Create a read buffer to transfer data from the input

BYTE [] BUF = New byte [4096];

/ / Reaffirm the entrance

Jarentry Entry;

While ((entry = jarin.getnextjarent ())! = null) {

// Discover the list file from the old JAR

IF ("meta-inf / manifest.mf" .equals (entry.getname ())).

/ / Write an entry for the output JAR

Jarout.putNextentry (Entry);

Int Read;

While ((Read = Jarin.Read (buf))! = -1) {

Jarout.write (buf, 0, read);

}

Jarout.closeentry ();

}

// Refresh and close all flow

Jarout.flush ();

Jarout.close ();

Jarin.close ();

Completion procedures

Of course, we must put this code in a class MAIN method and use an appropriate input declaration setting. The source program section provides a full program.

Use example

Let us use an example to put this program into actual use. Suppose you already have an application whose MAIN entry is in a class called HellorunnableWorld (this is a full name). At the same time, it is assumed that you have created a JAR file called myjar.jar and contains the entire application. Run makejarrunnable in this JAR file as in this JAR file as follows:

Java makejarrunnable myjar.jar HellorunnableWorld myjar_r.jar

As mentioned earlier, pay attention to how I gave the parameter list. If you have forgotten the order, just don't use the parameter to run this program, it will respond to a usage message. Try to run the java -jar command on myjar.jar and run on myjar_r.jar. Pay attention to different places! After doing this, browse the list file (Meta-INF / Manifest.mf) in each JAR. (You can find these two jars in the source code)

This has a suggestion: try to put the Makejarrunnable program into a acquired jar!

run

Running a JAR by double-clicking or using a simple command, it is convenient to contain it to be included in your class path and run a special main class. To help you do this, JAR specification provides a main-class property for JAR's manifest file. The procedure I introduced here makes you easy to operate this property and make your Jars get your JARS.

转载请注明原文地址:https://www.9cbs.com/read-84577.html

New Post(0)