JAVA jar command (a) -jar packaging class files

the jar package essentially all the class files, resource files labeled a compressed packet (may choose not to compress), optionally generated META-INF / MANIFEST.MF file jar package, is the list of the MANIFEST.MF file, which can recording main class, classpath and other information for the virtual machines.
The next period of time, the knowledge that we will learn the following path and command jar manifest file

    jar packaging class files
    with the package class jar file packaged
    using the manifest file

In this article we will use a simple program to become familiar with java jar use the command, because it is for the purpose of proficiency using the jar, the following operations may be verbose, re-use jar command, the following is the directory of the film article, Lite option jar command directly see

            the option of a jar command
                usage
                option
                used in this section to order
            two only one implementation of the executable jar file class
                method will extract all Mainjar modify MANIFESTMF file and then repackage the
                second method of generating jar package when assigned a manifest file
                method three is the jar package the list of documents specified
                method four for the update file is a list of jar files
            packaged plurality of three class of

options jar command.
usage:

Braces option is mandatory, in brackets is optional, jar-file is a jar file; manifest-file is manifest file, META-INF That jar package / MANIFEST.MF file

jar {ctxui} [vfmn0PMe] [JAR-file] [the manifest-file] [entry-Point] [-C dir] files ...

    1

option:

-c (the create) to create a new archive
-t list the archive directory
-x extract from the archive specific (or all) files
-u (update) to update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information specified in the manifest file
execution Pack200 normalization after you create a new file -n
-e is tied to a separate executable application jar file
    specified application entry point
-0 only memory; not use any ZIP compression
-P preamble reserved file name '/' (absolute path), and ".." (parent directory) component
-M manifest file does not create entries
-i generate index information for the specified jar file
-C change to the specified directory and contains the following files

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    9
    10
    11
    12
    13
    14
    15

used in this section to the command

jar uf xx.jar [file ... | path ] is to be noted that at the end of updating the jar file will generate a new manifest file, which in this article "method 4: in order to update the jar file, the file is a list of documents" in the examples illustrate

jar cf xx.jar [file ... | path ] will file such as a file or directory path packaged into xx.jar
jar cvf xx.jar [ file ... | path] Ibid., displays detailed information
jar cmf manifest-file xx.jar [file ... | path] will file such as a file or directory path packaged xx.jar, and to develop its manifest file
jar cMf xx .jar [file ... | path] will file such as a file or directory path packaged xx.jar, the package does not generate a manifest file
jar uf xx.jar [file ... | path ] will file such as a file or directory to update xx.jar, be sure to pay attention! ! ! This update will regenerate the manifest file
jar uMf xx.jar [file ... | path ] above, but does not generate a manifest file
jar umf manifest-file xx.jar {file ... | path} will file other documents or path directory updates to xx.jar
JAR TF xx.jar all the files listed xx.jar
jar xf xx.jar xx.jar to extract all the files in the current directory to the
jar xf xx.jar {file ...} xx.jar in the extracted file into the current directory and the like

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10

two only a realization executable jar file of the class

first create a folder jarDemo, which placed our test class

mkdir jarDemo
cd jarDemo
Touch Main.java

    1
    2
    3

add our hello world code Main.java inside, and javac compiler

{the main class public
    public static void main (String ... args) {
        System.out.println ( "Hello World");
    }
}

    . 1
    2
    . 3
    . 4
    . 5

the javac Main.java is

    1

is then generated Main.class packaging, c option is to create a new jar package, f is the name of the specified jar package

jar cf main.jar Main.class

    1

if the additional use of the v option, it lists detailed information package

main.jar Main.class cvf jar

    1

file we can view generated by the t option in the jar, the contents of the package is the output directory and file package, you can see the default generated MANIFEST.MF file

jar tf main.jar

output:
the META-INF /
the META-INF / MANIFEST.MF
Main.class

    . 1
    2
    . 3
    . 4
    . 5
    . 6

of course, we may not be generated by the M MANIFEST.MF file option, the package can be seen that only a file Main.class

rm Main. JAR
JAR CMF main.jar Main.class
JAR TF main.jar

output:
Main.class

    . 1
    2
    . 3
    . 4
    . 5
    . 6

Although we have Main.class packaged and Main.class have main (String ... args) entry method, but the jar or can not be performed because the virtual machine does not know which class in this package is the main method of

java -jar main.jar

output:
main.jar no master list of properties

    1
    2
    3
    4

below or revert to have MANIFEST.MF file main.jar package, we want him unzip, add the main class property in the MANIFEST.MF
decompression there are many methods jar package, our aim is to extract the MANIFEST.MF default view files generated, and make changes, and finally generate an executable jar package, because our aim is for skilled use jar named below us the use of multiple methods to achieve this goal
repackaged extract all the Main.jar, and then modify the MANIFEST.MF file: a method

because jar unzip command can only extract to the current directory, this file will cause confusion, we create a new folder uncompress the Main.jar copied to this folder and then unzip, you can see, jar package all the files, x option is put forward all or specified file, in this case Where all the documents are presented, in practice you can specify the file with the f option put forward, such as XF main.jar JAR META->> INF / the MANIFEST.MF

mkdir the uncompress
cp main.jar the uncompress
cd the uncompress
JAR XF main.jar
LS -RF

output:
INF-the META / Main.class main.jar

./META-INF:
the MANIFEST.MF

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10
    . 11

open META-INF / MANIFEST.MF file, see the default manifest file is very simple to generate, only two lines

Manifest-Version: 1.0
the Created-By: 1.8.0_101 (the Oracle Corporation)

    . 1
    2

add add the upper main class attribute, a colon attention must leave a space, and finally a line feed must stay

Manifest-Version: 1.0
By-the Created: 1.8.0_101 (the Oracle Corporation)
Main-Class: Main

    1
    2
    3

then then uncompress all directories, files entire package, pay attention to first delete the original main.jar, you can finally have run this new main.jar main class attribute it

RM main.jar
JAR main.jar CMF.
java -jar Main.jar

output:
the Hello world

    1
    2
    3
    4
    5
    6

Note that the above jar command this option is not used to generate a list of M, because the original uncompress directory there, or if using a jar cf command to generate the list above the first package generated list is the same, there is no main-class: main main class

RM main.jar
JAR CF2 main.jar.
Java -jar main.jar

output:
main.jar attribute master list no

    . 1
    2
    . 3
    . 4
    . 5
    6

method two: when generating jar package assign a list of documents

or the method of uncompress a directory, we MANIFEST.MF under the META-INF directory to the next uncompress, delete main.jar, and META-INF directory, so Main.class and uncompress only two MANIFEST.MF file, the main class is MANIFEST.MF manifest file attributes

. Music Videos the META-INF / the mANIFEST
RM main.jar
RM the META-INF -R & lt
LS -RF

output:
Main.class the MANIFEST.MF

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7

took a specified list of documents, generate Main.jar, m option to specify a list of documents

JAR CMF main.jar the MANIFEST.MF Main.class
Java -jar main.jar

output :
the Hello world

    1
    2
    3
    4
    5

method three: specify the manifest file for the jar package

also remember that the earliest jardemo directory list that there are no Main.jar main class for you, we have updated the main class manifest file for its
jardemo / uncompress MANIFEST.MF directory is the main class, we take it to the next jardemo directory

cd Desktop / jardemo
mv the uncompress / MANIFEST.MF.
RM -r the uncompress
LS -RF

output:
MANIFEST.MF Main.class main.jar main.java

    . 1
    2
    . 3
    . 4
    5
    6
    7

Use u option to update the jar package, using the m option to specify a list of files, output some warnings, because the new manifest file in the original package and Main.jar inventory information has two fields is the same name of

jar -umf MANIFEST .MF main.jar

output:
March 13, 2017 10:50:50 pm the Read java.util.jar.Attributes
the WARNING: Duplicate name in Manifest:. Manifest-Version
of Ensure that the manifest at The Duplicate entries It does not have have, and
that blank Individual Sections in separate both-Lines your
the manifest and in at The META-INF / entry in the MANIFEST.MF at The JAR File.
March 13, 2017 10:50:50 pm the Read java.util.jar.Attributes
the WARNING: Duplicate name in Manifest: By-the Created.
of Ensure that the manifest at The Duplicate entries It does not have have, and
that blank Lines separate Individual Sections in both-your
. The the manifest and in the META-INF / entry in the MANIFEST.MF The JAR File

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10
    . 11
    12 is
    13 is

then run JAR

Java -jar main.jar

output:
Hello World

    . 1
    2
    . 3
    . 4

FOUR : to update jar file, the file is a list of files

using this method primarily to familiarize yourself with the options again u understand the concept and the package
we have deleted the list of the main class of Main.jar in jardemo directory, again generated no first MANIFEST.MF main.jar main class

JAR CF2 main.jar Main.class

    . 1

jar package MANIFEST.MF in the main class records, but only a MANIFEST.MF file jar package, we will Main.jar no MANIFEST.MF replace the main class for the MANIFEST.MF and methods have three main classes the same effect is, first of all talk about the main categories of MANIFEST.MF in the right path which we saw before the jar files in the package is placed in the META-INF directory, use u to update the jar, attention to the need to use M to ignore the manifest file

mkdir the META-INF
Music Videos the MANIFEST.MF the META-INF
JAR UMF main.jar the META-INF / the MANIFEST.MF
Java -jar main.jar

output:
Hello World

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7

. more than three class packaged

one on in, jarDemo directory has Main.java class information and main manifest file MANIFEST.MF, we keep only two all documents, all other deleted, and then create a new Say.java file

ls -RF

output:
the META-INF / Main.java is Say.java

./META-INF:
the MANIFEST.MF

    . 1
    2
    . 3
    . 4
    . 5
    . 6
    . 7

in the new Say.java:

public class {Say
    public static void say (String STR) {
        System.out.println ( "This IS" + STR);
    }
}

    . 1
    2
    . 3
    . 4
    . 5

modify Main.java as so Say.say it calls Say class (...) method

public class main {
    public static void main (String ... args) {
        System.out.println ( "the Hello world");
        Say.say ( "Charles");
    }
}

    . 1
    2
    . 3
    . 4
    . 5
    . 6

we compile documents and packaging, note that the main class specifies a property the MANIFEST.MF

the javac * .java
JAR CMF main.jar the META-INF / Main.class Say.class the MANIFEST.MF
java -jar Main.jar

输出:
hello world
This is Charles

Guess you like

Origin www.cnblogs.com/YZFHKMS-X/p/11840281.html