This article has participated in the weekend study program, click the link to see details :juejin.cn/post/696572…

If the profile

When you run a packaged application using Java -jar yourjarexe. jar, you will find that the application with the -classpath parameter cannot find the corresponding third-party class. Error ClassNotFound is reported. In fact, this is due to the fact that when run with the -jar argument, the Java VM masks all external classpath and only scopes the class with its own internal class in yourJarexe.jar.

The solution

BootStrap Class loader extension scheme

The -xbootclasspath parameter is used to load the extended JAR package.

The Java command line provides a simple way to extend the Bootstrap-level class.

  • -xbootclasspath: A Java class search path that completely replaces the basic core. It is not commonly used, or you must rewrite all the Java core classes (not recommended!). .

  • -xbootclasspath /a: Suffix after the core class search path (recommended)

  • -xbootclasspath /p: The prefix is before the core class search path and is not used frequently to avoid unnecessary conflicts. (Not recommended)

The syntax is as follows: (The separator is similar to the classpath argument. Unix uses the: number, Windows uses it. Number, for example, Unix.)

 java -Xbootclasspath/a:/usrhome/thirdlib.jar: -jar yourJarExe.jar
Copy the code

Extensibility class loaders

Extensibility class loaders are generally stored in {Java_home}\jre\lib\ext, and the search for extensibility class paths is automatic when Java is called. The solution is simply to copy all the third party JARS you want to use into the ext directory. Will be transparent into the load category.

Note: Not recommended, otherwise all JReS need to be migrated and unified and synchronized!

User Class extension scheme

When an executable JAR package is executed using -jar, the JVM sets the directory where the jar package resides to the codebase directory, where all class searches begin.

So if you use a third party JAR package, a more acceptable configuration solution is to use the Manifest extension mechanism of the JAR package.

Complete the following steps

  1. Copy the required third-party JAR packages to the same directory or subdirectory as the executable JAR. For example, if the jar package is in /usrhome/ yourjarexe. jar, you can copy all the jar packages to /usrhome or /usrhome/lib or similar subdirectories.

  2. Modifying the Manifest file

Add the following line to the manifest.mf file

 Class-Path:classes12.jar lib/thirdlib.jar
Copy the code

Class-path is the keyword that the executable JAR package runs on. Detailed content can refer to

Java.sun.com/docs/books/… .

Note that class-path is simply an abbreviation for the CLASSPATH environment variable on your local machine. This means that the prefix is used to look for the appropriate third-party Class/library in all of the CLASSPATH directories on your jar execution machine.

  • You cannot use class-path to load jar files in your own JAR package (or on the network).

  • In theory, your JAR distribution should not include other third-party libraries (instead, use instructions to remind users to get the supporting libraries).

  • If you need to distribute other third-party libraries (JARS, zip, classes, etc.) directly in your own JAR package, you will have to implement a custom ClassLoader to load the third-party libraries in your own way.