Java Build jar Package

Source: Internet
Author: User

Whether the application program written by Java can finally form an EXE-like executable file. Can you just execute it with the command line?

There are usually two kinds. One is to make a runnable jar file package. You can then double-click to run like a. chm document, and another is to use jet to compile.

Jet was paid for, and it is said that jet was not able to compile all of the Java programs into a running file, and that performance would have to be discounted. So, the best way to make a run of a jar file package is to keep the Java cross-platform feature.

Here's a look at what a jar file package looks like:

1. jar File Package

The jar file is Java Archive file, as implies meaning. Its application is related to Java and is a document format for Java. Jar files are similar to zip files--exactly. It is a zip file, so call it a file package.

The only difference between a jar file and a zip file is that it includes a meta-inf/manifest in the contents of the jar file. MF file, this file was created on its own initiative when generating the jar file.

Give me a sample example. Suppose we have some files such as the following folder structure:
==
--Test
--Test.class
Compress it into a zip file Test.zip, the internal folder structure of this zip file is:
Test.zip
'--Test
'--Test.class

Assuming that we use the JDK jar command to call it a jar file package Test.jar, the internal folder structure of this jar file is:
Test.jar
|--Meta-inf
| '--MANIFEST. Mf
'--Test
'--test.class
2. Create a runnable jar file package

Creating a runnable Jar file package to advertise your program is the most typical use of jar packages.

A Java program is made up of several. class files. These. class files must be classified according to the package they belong to, and you need to assign the root folder of all the packages used to the CLASSPATH environment variable or the-CP of the Java command, and execute it with the Java command on the console, assuming that you need to write directly by double-clicking Execute Windo A batch file (. bat) for WS or a shell program for Linux.

As a result, many people say that Java is a user-friendly programming language that makes it easy for developers to suffer.

In fact otherwise, suppose the developer can make a runnable jar file package to the user. Then it is convenient for users to use them.

When installing the JRE (Java Runtime Environment) under Windows. The installation file maps the. jar file to Javaw.exe open.

So, for a runnable jar file package, the user simply needs to double-click it to run the program. As easy as reading. CHM documents (. CHM documents are opened by hh.exe by default). The key now, then, is how to create this runnable jar file package.

Create a runnable jar file package. You need to use the jar command with CVFM parameters, as in the test folder above, for example, commands such as the following:

Jar CVFM Test.jar MANIFEST.MF test

Here Test.jar and manifest.mf two files, each corresponding to the respective number F and M, whose plays are in MANIFEST.MF. Because you want to create a runnable jar file package. It is not enough to specify a MANIFEST.MF file, and because manifest is a feature of the jar package, both the runnable jar package and the non-running jar package include manifest. The key is to run the manifest of the jar file package. Its contents include Main-class. This is written in manifest format such as the following:

Main-class: The full name of the main class can be run (including the package name)

For example, if the Test.class in the example above is part of the test package. And is a class that can be run (the public static void Main (string[]) method is defined), then this manifest.mf can be edited such as the following:

Main-class:test. Test < Enter >

This manifest.mf can be placed in whatever position, can be other file name, only need to have main-class:test. Test line, and the line ends with a carriage return. After the MANIFEST.MF file has been created. Our folder structure changes to:
==
|--Test
| '--Test.class
'--MANIFEST.MF

In this scenario, you need to use the jar command in the parent folder of the test folder to create the jar file package. That is, in the folder tree using the "= =" in the folder that is represented, such as the following command is used:

Jar CVFM Test.jar MANIFEST.MF test

The Test.jar is then created in the "= =" folder, and this test.jar is the jar file package that is executed. It is only necessary to use the Java-jar Test.jar command when executing.

It is important to note that the jar package created needs to include a complete folder structure corresponding to the package structure of the Java program. Just like the example above. The classes specified by Main-class must also be complete, including the class name of the package path. As in the above example, test. Test. And you can use Java < class name > to execute this class without hitting the jar file package. That is, in the example above, Java test. Test is able to execute correctly (of course, in the case of CLASSPATH correct).

Java Build jar Package

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.