Generate a jar package in java and generate a jar package in java

Source: Internet
Author: User

Generate a jar package in java and generate a jar package in java

Can an application program written in Java eventually form an executable file similar to exe? Can it only be run through command lines?
---------------------------------------------------------------

There are usually two types: one is to create an executable JAR file package, and then double-click it to run like the. chm file; the other is to use JET for compilation. However, JET has to pay for it, and it is said that JET has not compiled all Java programs into execution files, and the performance has to be discounted. Therefore, the best choice is to create an executable JAR file package. Besides, it can maintain Java's cross-platform features.

Let's take a look at what is a JAR package:

1. JAR package

JAR files are Java Archive files, which are closely related to Java and are a document format of Java. JAR files are very similar to ZIP files-specifically, they are ZIP files, so they are called file packages. The only difference between a JAR file and a ZIP file is that the content of the JAR file contains a META-INF/MANIFEST. MF file, which is automatically created when the JAR file is generated. For example, if we have files with the following directory structure:
=
-- Test
-- Test. class
To compress the ZIP file into test.zip, the internal directory structure of the ZIP file is as follows:
Test.zip
'-- Test
'-- Test. class

If we use the JDK jar command to compress it into the JAR package test. jar, the internal directory structure of the JAR file is:
Test. jar
| -- META-INF
| '-- MANIFEST. MF
'-- Test
'-- Test. class
2. Create an executable JAR package

Creating an executable JAR package to publish your program is the most typical usage of the JAR package.

A Java program is composed of several. class files. These. class files must be stored in different directories according to their packages. Before running the class files, you must specify the root directories of all used packages to the-cp parameters of the classpath environment variables or java commands; when running, go to the console and run the java command. If you need to directly double-click to run the command, you must write the Windows batch processing file (. bat) or Linux Shell program. Therefore, many people say that Java is a programming language that facilitates developers to suffer from users' difficulties.

Otherwise, if the developer can make an executable JAR package and hand it to the user, it will be convenient for the user to use. When JRE is installed in Windows (Java Runtime environment.pdf, the installation file will map .jarfile to javaw.exe. For an executable JAR package, you only need to double-click it to run the program. It is as convenient as reading the. chm document (. chm document is opened by hh.exe ). Now, the key is how to create the executable JAR package.

To create an executable JAR file package, you must use the jar command with the cvfm parameter. The command is as follows:

Jar cvfm test. jar manifest. mf test

Here, the test. jar and manifest. mf files correspond to the f and m parameters, and their names are highlighted in manifest. mf. To create an executable JAR file package, specify a manifest. mf files are not enough because MANIFEST is a feature of the JAR package. Both executable JAR packages and unexecutable JAR packages contain MANIFEST. The key lies in the MANIFEST of the executable JAR package, which contains the Main-Class item. The writing format in MANIFEST is as follows:

Main-Class: full name of the executable Main Class (including package name)

For example, assume that Test. class belongs to the test package and is an executable class (defines the public static void main (String []) method), then this manifest. mf can be edited as follows:

Main-Class: test. Test <press enter>

This manifest. mf can be put anywhere or another file name. You only need a line of Main-Class: test. Test and the line ends with a carriage return. After the manifest. mf file is created, our directory structure becomes:
=
| -- Test
| '-- Test. class
'-- Manifest. mf

At this time, you need to use the jar command in the upper-level directory of the test directory to create a JAR file package. That is, in the directory indicated by "=" in the directory tree, use the following command:

Jar cvfm test. jar manifest. mf test

Then, test. jar is created in the "=" directory. This test. jar is the JAR file package that is executed. You only need to run the java-jar test. jar command.

Note that the created JAR package must contain a complete directory structure that corresponds to the Java program package structure, just as in the preceding example. The Class specified by Main-Class must also be a complete Class name containing the package path. test; and you can use java <Class Name> to run this class before it is packaged into a JAR file, that is, java test. test can run correctly (of course, when CLASSPATH is correct ).


Java project to generate a jar package

Compilation error?

Java project to generate a jar package

Compilation error?

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.