How to run the jar package correctly

Source: Internet
Author: User

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

Are you using the Java-jar parameter to run the packaged jar application?ProgramWhen I found that the application could not find the content of the third-party class library set under classpath? No matter how you set the classpath parameter, the classnotfound error is always reported? This post will help you get rid of your troubles.
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.
(Self-evaluation: it is not recommended and has poor adaptability !)

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 executable jar package running dependency. For details, refer to: http://java.sun.com/docs/books/tutorial/ext/index.html

The first method is recommended for the above three methods, with 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, please Google Custom classloader.

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.

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.