-How to set the classpath when the jar parameter is used to run the application

Source: Internet
Author: User
Tags bootstrap classes

 

-When the jar parameter is used to run the application, the classpath setting method is posted by zeal on, 3586 characters | + 1-0 English reprint. Please keep the original source declaration information of the Bank: http://www.zeali.net/entry/15made1nzealtag (Java/C ++ ):
Jar, classpath, class library, classloader

When Java-jar yourjarexe is used. jar to run a packaged application, you will find that no third-party class can be found in the classpath parameter application, and the classnotfound error is reported. In fact, this is because when the-jar parameter is used for running, the Java VM will block all external classpath, and only use the internal class of yourjarexe. jar as the scope of the class.

** Solution **

One Bootstrap class extension scheme

The Java command line provides a simple method for extending the bootstrap class.
-Xbootclasspath: Completely replaces the Java class search path with the basic core.
Not commonly used. Otherwise, you must re-write all Java core classes.
-Xbootclasspath/A: The suffix is behind the core Class search path. It is commonly used !!
-Xbootclasspath/P: prefix before core Class search path. not commonly used. Avoid
Cause unnecessary conflicts.

Syntax:
(The Delimiter is similar to the classpath parameter. UNIX uses the "#" And Windows uses the "#". Here, UNIX is used as an example)
Java-xbootclasspath/A:/usrhome/thirdlib. jar:-jar yourjarexe. Jar

2. Extended extended solution of extend class

Java exten class is stored in the {java_home} \ JRE \ Lib \ ext directory. when Java is called, the search for extended class paths is automatic. always search. in this way, the solution is simple. Copy all third-party jar packages to the ext directory.

Three-user class extension scheme

When you use-jar to execute an executable jar package, JVM sets the directory where the jar package is located to the codebase directory where all class searches start. therefore, if other third-party jar packages are used, a more acceptable configuration scheme is to use the manifest extension mechanism of the jar package.
The procedure is as follows:

1. copy the required third-party jar package to the directory or a subdirectory where the executable jar is located. for example, the jar package is in/usrhome/yourjarexe. jar, you can copy all the jar packages to the/usrhome directory or a directory similar to/usrhome/lib.

2. Modify the manifest File

Add the following lines to the manifest. MF file:

Class-path: classes12.jar lib/thirdlib. Jar

Class-path is the keyword of the executable jar package running dependency. For details, refer to the http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html. Note that class-path is just an abbreviation of the classpath environment variable on your local machine, that is to say, use this prefix to search for the corresponding third-party classes/Class Libraries under all the classpath directories on your jar package Execution Machine. You cannot pass
Class-path to load jar files located in your own jar package (or on the network. Theoretically, your jar release package should not contain other third-party class libraries (Instead, you should use instructions to remind users to obtain corresponding support class libraries ). If other third-party libraries (such as jar, zip, and class) must be directly packaged and published together in your own jar package due to special requirements, you must implement a custom classloader to load these third-party class libraries according to your own intentions.

The above three methodsRecommended first, Good scalability, and the most convenient operation.
In addition, writing your own classloader to dynamically load the class is more complex and advanced technology. I will not repeat it here. if you are interested, go to Google Custom classloader or refer to another log of mine: Let the classpath parameter go away.

The security mechanism of Java varies with different JDK versions and affects many core classes, such as thread. Therefore, many large commercial software require strict JDK versions. this is partly because of this. this also requires that you describe the JDK version for development and testing regardless of the size of your own applications.

The method described in this article is based on j2sdk 1.4.2 _ 04-b05

Bytes ----------------------------------------------------------------------------------------------

Appendix: Background

Since JDK 1.2, JVM has adopted the delegate (delegate) mode to load the class. The reason for this design can refer to the http://java.sun.com/docs/books/tutorial/ext/basics/load.html

To sum up, it provides a customizable security mechanism for the Application Layer Based on the JVM sandbox installation model.

The sequence in which the Java Virtual Machine (JVM) looks for Classes

1. Bootstrap classes

The core class of the Java platform, such as Java. lang. string. and RT. jar and other important core-level classes. this is loaded by JVM Bootstrap class loader. usually placed in the {java_home} \ JRE \ lib directory

2. extension classes

The Java extension mechanism is used to expand Java core functional modules. For example, Java serial communication module comm. jar is generally placed in the {java_home} \ JRE \ Lib \ ext directory.

3. User classes

Java packages developed by developers or other third parties. use the-classpath or-CP command line, or set the classpath environment variable to reference. by placing the JVM in {java_home} \ Lib \ tools. jar to find and call user-level class. the commonly used javac also calls tools. jar to find the path specified by the user to compile the Java source program. this leads to the order or priority of the user class path search.

3.1 default value: Call the current path (.) of Java or javawa, which is the current directory of the developed class.
3.2 path set for the classpath environment variable. If classpath is set, the value of classpath overwrites the default value.
3.3 execute the value of the Java command line-classpath or-CP. If one of the two command line parameters is specified, its value will overwrite the value of the environment variable classpath.
3.4-jar option: If you use Java-jar to run an executable jar package, the current jar package will overwrite all the above values. in other words, the priority of the jar package after-jar is the highest. If the-jar option is specified, all the environment variables and the search path created by the command line will be ignored. JVM appclassloader will only use the jar package as the search range.
The executable jar has many security-related descriptions, you can refer to the http://java.sun.com/docs/books/tutorial/jar/ for a comprehensive understanding.

This is why the third-party jar package cannot be referenced no matter how you set classpath after the application is packaged into an executable 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.