Java application Packaging

Source: Internet
Author: User



Jar command Java Archive File, java Archive File. This is similar to a zip file.
Usage: jar {ctxu} [vfm0Mi] [jar-file] [manifest-file] [-C Directory] File Name...
Option:
-C. Create a new archive
-T list the archived content
-X expand the named (or all) files in the archive
-U: update an existing archive
-V generates detailed output to the standard output
-F specifies the archive file name
-M contains the marker information from the specified file.
-0 storage only; ZIP compression format not used
-M does not generate a manifest file for all items
-I generates 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 is recursively processed.
Both the configuration (manifest) file name and the archive file name must be specified, in the same order specified by the 'M' and 'F' signs.

Example 1: Archive two class files to an archive file named 'classes. jar:
Jar cvf classes. jar Foo. class Bar. class
Example 2: use an existing manifest file 'mymanifest'
Archive the file to an archive file named 'classes. jar:
Jar cvfm classes. jar mymanifest-C foo /.


Java application package into a jar package, in the jar package directory under the META-INF MANIFEST. MF file specifies the Main-class entry when the Jar package is executed, Main-Class: XXX. Xx

Jar (test. jar) if you need to reference other third-party jar packages, reference this file in eclipse as a project jar package. jar package, which was put under the lib subdirectory of the project at that time, and some. jar, but java-jar is used to execute the test. when the jar file is used, the system reports that the Class cannot be found because the jar file cannot be referenced in its own jar file.


Can I add it to classpath at runtime? Add the classpath parameter while running the jar file:

Java-classpath some. jar-jar test. jar

This method is not feasible because the jar specified by classpath is loaded by AppClassloader. After the-jar parameter is added to the java command, AppClassloader only pays attention to test. the classpath parameter is invalid because the class is in the jar range.

How can I reference other jar packages?

Method 1: use Bootstrap Classloader to load these classes

We can use the following parameters at runtime:

-Xbootclasspath: Completely replaces the system's Java classpath. It is best not to use it.
-Xbootclasspath/a: loaded after the system class is loaded. This is generally used.
-Xbootclasspath/p: It is loaded before the system class is loaded. Pay attention to the usage. conflicts with the system class are not good.

Win32 java-Xbootclasspath/a: some. jar; some2.jar;-jar test. jarunix java-Xbootclasspath/a: some. jar: some2.jar:-jar test. in jarwin32 systems, each jar is separated by a semicolon, and in unix systems, it is separated by a colon.

Method 2: Use Extension Classloader to load

You can throw all the jar files to be loaded to % JRE_HOME %/lib/ext. The jar package in this directory will be loaded by the Extension Classloader after the Bootstrap Classloader is finished. Very convenient and worry-free. :)

Method 3: Use AppClassloader for loading, but the classpath parameter is not required.

Add the following code to MANIFEST. MF: Class-Path: lib/some. jar.

Lib is a sub-directory in the same directory as test. jar, where some. jar packages to be referenced by test. jar are located. Then test and run. Everything is normal!

If multiple jar packages need to be referenced: Class-Path: lib/some. jar lib/some2.jar

Separate each jar with spaces. Use relative paths.

Also, the Class-Path configuration may become invalid if the META-INF contains the INDEX. LIST file. INDEX. LIST is the INDEX file generated when the Jar packaging tool is packaged. deletion does not affect the operation.

Method 4: Customize Classloader to load

This method is the ultimate solution. Basically, well-known java applications are doing that, such as tomcat and jboss.

This method is a bit complicated and requires special discussion. For the principle of ClassLoader and custom ClassLoader, refer to this http://cuixiaodong214.blog.163.com/blog/static/951639820099135859761

Summary:

The above four methods can be used, especially when the program runs in a very simple environment. However, when running in multi-task and multi-application environments, it is recommended that each application be independent of each other. Both the first and second solutions may affect other applications, therefore, it is best to select the third and fourth types.


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.