Make a jar file that cannot be run can be run using Java-jar

Source: Internet
Author: User

Summary

This article shows you how to make a jar that is not available to run and not manipulate the manifest file directly. You will learn to develop a short program so that any jar file can be run using the Java-jar command or by double-clicking on Windows like.

You can package all the classes and resources of an application into a jar file. In fact, that's one of the purposes of the jar file. Another goal is to make it easy for users to perform applications stored in jar files, so why should we let them just assume the functionality of the package and become second-class citizens in the Java world when they can be a first-class citizen and equate to native executables?

To execute a JAR file, you can use the-jar option of the Java command. For example you have a running file named Myjar.jar jar file, because it is operational and you can perform it like this: Java-jar Myjar.jar

In addition, when the JRE is installed on an operating system such as Windows, you can double-click the jar file to run the application after you associate it with the JVM. These jars must be operational.

The question is: how can you make a jar run?

Manifest files and Main-class entries

In most jar files, there is a MANIFEST.MF file in the Meta-inf directory with a special entry main-class in that file, which tells the Java-jar command to execute the class.

The problem is that you have to properly add this particular entry to the manifest file: it must be in a specific location and must conform to a specific format, while some people do not like editing the configuration file.

Let the API do it for you

Starting with Java 1.2, the Java.util.jar package allows you to manipulate the jar file (note: It is based on the Java.util.zip package). More precisely, Java.util.jar allows you to easily manipulate that particular manifest file through the manifest class.

Let's write a program that uses that API. First of all, this program must know three things:

1. We want to be able to execute the jar

2. The main class that we want to execute (this class must exist within the jar)

3. The filename of the new jar file, because we should not simply overwrite those files

Writing Programs

The list above will form the parameters of our program, and based on this, let's pick a suitable name for the application. How does makejarrunnable sound?

Check Main's parameters

Assuming that our main entrance is a standard main (string[]) method, we should first check the parameters of the program:

if (args.length != 3) {
     System.out.println("Usage: MakeJarRunnable "
              + "<jar file> <Main-Class> <output>");
     System.exit(0);
   }

Note how the argument list is interpreted, as it is important for subsequent code. The order and content of the parameters are not rigidly set, but if you change them, remember to modify the other code as appropriate.

Accessing the jar and its manifest files

First we must create some objects that know the jar and manifest files:

//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 exists
   manifest = new Manifest();
   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.