Load a third party JAR in the active JAR

xiaoxiao2021-04-01  193

First, in the foreword is often published in the development of the class into the JAR package in commercial development. These features are often used because there are many third-party provider set JAR packages. The following scenarios are many Java developers often encountered: in the development, commissioning phase, by setting a third-party JAR packet path in ClassPath, the Class works work is normal. When the development is complete, when deployed, the development of the developed class into a JAR package will find ClassLoader.getsystemClassLoader () or classloader.getsystemclassloader (). LoadingClass (String classname) to dynamically load existence in third-party JAR Class in the package will throw an exception "ClassNotFoundException.

This problem exists to run the JAR package through jar -jar yourself.jar, and dynamically load the third party's class through the class.Forname (String ClassName) in the ClassName in JAR.

Common applications, such as based on the user's selection configuration, dynamically load a different manufacturer's JDBC Driver.

Second, background knowledge

Since JDK 1.2, JVM uses a delegate mode to load Class. The reason for this design can be referred to

http://java.sun.com/docs/books/tutorial/ext/basics/load.html

Incident: It is a customizable security mechanism for providing application layers on the JVM Sandbox installation model.

Third, Java Virtual Machine (JVM) looks for the order of CLASS

3.1 Bootstrap Classes

Class of Class, Java.lang.String, etc., such as Java.lang.String, etc., and RT.jar, etc., is loaded by JVM Bootstrap Class Loader. Generally placed in {java_home} / jRE / LIB Under contents

3.2 Extension Classes

Based on the Java expansion mechanism to extend the Java core functional module. For example, the Java serial communication module comm.jar. Generally placed in {java_home} / jre / lib / ext directory

3.3 User Classes

Developer or other third-party Java package. By command line-ClassPath or -cp, or by setting the ClassPath environment variable. JVM is placed in {java_home} /lib/tools.jar to find and call user-level Class. Common Javac is also looking for user-developed Java source programs by calling Tools.jar. This leads out the order or priority of the User Class path search.

3.3.1 Default: Call the current path (.) Of Java or Javaw (.), Is the current directory where the developed class exists.

3.3.2 The path to the ClassPath environment variable. If ClassPath is set, the value of the classpath overrides the default value.

3.3.3 Executing the value of the Java command line-ClassPath or -cp, if one of these two command line parameters is developed, its value overrides the value of the environment variable ClassPath

3.3.4 -JAR Options: If you run an executable JAR package through java -jar, this current JAR package overrides all the values ​​above. In other words, the priority of the JAR package followed by -jar is the highest, if Specify the -jar option, all environment variables and command lines to be ignored. JVM AppClassLoader will only be a search range as a JAR package. For the executive JAR has many related security descriptions, you can refer to

Http://java.sun.com/docs/books/tutorial/jar/ is fully understood.

This is also why the application is packaged into an executable JAR package that cannot reference the third party JAR package.

Fourth, solution.

First we briefly summarize it, in order to make it easy to understand, simplify the architecture described above.

Java defines three levels of Class, which is Bootstrap Class, Extend Class, User Class. The User Class restriction rule is the most complicated. The JAR package that can be executed in the user class, more independent security rules, so solution based on Java three There are three different programs in different levels of Class extension mechanisms.

4.1 Bootstrap Class extension plan

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

-XBootClassPath: Basic core Java class search path. Not common, otherwise rewrote all Java core Class

-XBootClassPath / A: The suffix is ​​behind the core Class search path. Common.

-XBootClassPath / P: Prefix in front of the core Class search path. Not common, avoid unnecessary conflicts.

The syntax is as follows:

Java-xbootclasspath / a: /path/myclass/account.jar: -jar yourself.jar (unix)

Java-xbootclasspath: / d: /myclass/account.jar; -jar yourself.jar (window)

4.2 Extend Class extension plan

Java EXTEN CLASS is stored in the {java_home} / jre / lib / ext directory. When you call Java, the search for the extended Class path is automatically. This will always search. This way, the solution to the solution is very simple, will all want The third party JAR package used is copied to the ext directory.

4.3 USER CLASS extension scheme

When executing the executable JAR package using the -jar, JVM sets the JAR package to the CodeBase directory, all Class Searches Start in this directory. So if you have used other third-party JAR packets, a comparison can be accepted The configuration scheme is to use the Manifest extension mechanism for the JAR package. Steps

1. Several JAR packets that need to be required, copy it in the directory where you can perform JAR, or a subdirectory.

For example: JAR package is in d: /crm/luncher.jar then you can copy all JAR packets to the D: / CRM directory or D: / CRM / lib subdirectory.

2. Modify the Manifest file

Add to the manifest.mf file

Class-path: classes12.jar lib / class12.jar

Class-path is a keyword that can perform JAR package operation dependent. Details can be referenced

http://java.sun.com/docs/books/tutorial/ext/index.html

In addition, write your own ClassLoader to dynamically load Class, which is more complex and advanced technology. Limited to the space, do not repeat it. Interested to learn Google Custom ClassLoader

4.4 Recommended Program 4.1, the scalability is very good.

Five, summary

Java's security mechanism varies from different JDK versions, which will affect a lot of core Class, such as Thread, so many large commercial software, requiring JDK's version very strict. Part reasons are here. This also requires you write yourself. Application, regardless of the size, the JDK version of the development and testing is required.

JDK 1.4.1_03 for Windows is tested in this article.

转载请注明原文地址:https://www.9cbs.com/read-131182.html

New Post(0)