Create an executable JAR file

xiaoxiao2021-03-06  60

Create an executable JAR file

Assuming that we have created a GUI program, but we use the java command to start from the command line. Obviously, this will make Windows users feel uncomfortable. A better solution is to pack this (or these) Class files into an executable JAR file. Specifically, we can perform in accordance with the following steps:

1. Create a manifest.mf file This file is at least the following three lines: Manifest-Version: 1.0 main-class: Windumper's first row places the version of the manifest file, the second line is placed in main-class, Java Starting from its main function (this also shows the Class of the main-class property labeled ----- in the above example is Windumper (note that the Class extension) ---- must have a main function). Three lines are an empty line. If there is no this space, run the generated JAR file will not succeed. You can refer to Javadoc (DOCS / Guide / Jar / Jar.html) about the manifest file. 2. Create using the jar command. Like the JAR file, the JAR command corresponds to the utility JAR (bin / jar.exe in the Java installation directory). Type JAR and Enter in the command line, you can see how the JAR command is used and two examples. :

Usage: jar {ctxu} [vfm0mi] [jar- file] [manifest- file] [-c directory] file name ...

Option:

-c Create a new archive

-t lists the list of archived content

-x Expand Named (or all) files in the archive

-u Update existing archive

-v generation detailed output to standard output

-f specified archive file name

-m includes indicated information from the indicated file

-0 storage mode; unused zip compression format

-M does not generate a list of all items (Manifest] file

-i generate index information for the specified JAR file

-C changes to the specified directory and contains the following files:

If a file name is a directory, it will be processed.

The list (Manifest) file name and archive file name need to be specified, press the same order as specified by 'm' and 'f' flags.

Example 1: Archive two Class files into an archive file called 'classes.jar':

Jar cvf classes.jar foo.class bar.class

Example 2: Use an existing list (Manifest) file 'mymanifest' All Foo / Directory All

The file archive into a archive file called 'classes.jar':

JAR CVFM Classes.jar mymanifest -c foo /.

Based on these two useful examples, we can easily master JAR commands. Need to remind, you need to pack all covered packages when you pack Class. For the system, such as javax.swing. JTextField and the like, you don't have to pay attention to it. When you use attached CLASS, you have to ensure this. Below is an example: c: / documents and settings / eboy / desktop> JAR CVFM WINDUMPER.JAR MANIFEST.MF WINDUM

PER.CLASS WINDUMPER $ 1.Class Windumper $ 2.class -c D: / Myjava COM / ZG / UTIL / Console.c

Lass-C D: / Myjava COM / ZG / UTIL / MYSQLCONNECTION.CLASS -C E: / MMMYSQL ORG / GJT / MM / MYS

QL / -C D: / Myjava COM / ZG / UTIL / DUMPER.CLASS

Here we will name the generated JAR file to Windumper.jar and use the manifest.mf file created in front. The parameters after this parameter are used to indicate the file to be packaged (may not be a Class file, such as package pictures, etc. Resources Here, we compile the Windumper.java source program to generate Windumper.class, Windumper $ 1.class and Windumper $ 2.class three class files, it is clear that these three must be packaged. In addition, we still in Windumper Use console.class under the D: / Myjava / COM / ZG / Util folder, and the first non-invasive statement of the original file corresponding to this class is: package com.zg.util; then, we use parameters -C changes the directory to D: / Myjava and follow COM / ZG / UTIL / Console.class to complete the package. "- CD: / myjava com / zg / util / mysqlconnection.class is similar Complete the packaging of com.zg.util.mysqlconnection.class. Because the Windumper class uses the MySQL database, you must package the class (such as a DRIVER class) involved in the database. Since these classes may be referenced, we will E: All files in / mmmmySql / org / git / mm / mysql / directory are packaged. Are you fully packaged? This can't be learned by running JAR files, if you, you may get the following results:

Or a zombie interface. If you encounter these situations, you have to consider whether you pack all the classes. We can test this question like this:

Use the command line mode: java -jar yourjarfile.jar

To run the JAR file, if you have any exceptions, you can receive a message on the console.

After the packaging is completed, the generated windumper.jar file has such a file layout (because a JAR file is a compressed file, we can use the tools such as WinRAR to open it):

Placing a manifest.mf file in a folder called Meta-INF, but this file has been modified, we look at its contents: Manifest-Version: 1.0 Created-by: 1.4.2 (Sun Microsystem Inc) .) Main-class: windumper

The file has been added to the create-by attribute, and the file last is 2 empty lines. We simultaneously discovered such a directory structure: COM / ZG / UTIL / and ORG / GJT / MM / MySQL / this to add files to us The same (thinking about the addition of COM / ZG / UTIL / Console.Class). [I can imagine if we don't use the -c parameter, but write D: / myjava / com / zg / util / console. Class, the D: / Myjava / COM / ZG / UTIL / this directory structure will appear in the JAR file, which will cause certain classes that cannot be loaded].

3. Execute the JAR file Double-click on the JAR file generated above, and Javaw.exe will open it.

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

New Post(0)