Load third party JAR (repost) in executable JAR

xiaoxiao2021-04-01  199

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 is working properly. 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 The Class in the package will throw an exception "ClassNotFoundException. This problem exists in running JAR package through jar -jar yourself.jar, and dynamically load third-party through class.forname (String Classname) in the ClassName in JAR. Class time. Common applications, such as based on the user's selection configuration, dynamically load different vendors' JDBC Driver. Second, the background knowledge is from JDK 1.2, and JVM uses the delegate mode to load Class.

This design can be referred to http://java.sun.com/docs/books/tutorial/ext/basics/load.html: it is based on the JVM Sandbox installation model provides application layer. Customizable safety mechanism. Third, Java Virtual Machine (JVM) Look for the order of Class 3.1 Bootstrap Classes belongs to the core of the Java platform, such as java.lang.string, etc., and RT.jar and other important core levels of Class. This is Loaded by JVM Bootstrap Class Loader. Generally placed in {java_home} / jre / lib directory 3.2 Extension Classes based on Java expansion mechanism to extend Java core functional modules. For example, the Java serial communication module comm.jar. General placement In the {java_home} / jre / lib / ext directory 3.3 User class developer or other third-party Java package. By the command line-ClassPath or -cp, or by setting the ClassPath environment variable. JVM is placed {java_home} /lib/tools.jar to find and call user-level Class 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. Problem. 3.3.1 Default: Call the current path of Java or Javaw (.), Is the current directory 3.3.2 ClassPath environment variable setting path. If ClassPath is set, the ClassPath value will Override the default value 3.3.3 Execute the value of the Java command line-ClassPath or -cp, if one of these two command line parameters, its value will override the value of the environment variable ClassPath 3.3.4 -jar option: if Run an executable JAR package via java -jar, which current JAR package covers all the values ​​above. In other words, the priority level of the JAR package followed by -jar is the highest, if the -jar option is specified, all the environment The search paths set by the variables and command lines will be ignored. JVM AppClassLoader will only be in the search range as a JAR package. About the implementation of JAR has many related security descriptions, you can refer to http://java.sun.com/ Docs / books / tutorial / jar / to fully understand. Why should it be After being packaged as an executable JAR package, the reason for the third party JAR package cannot be referenced. Four, solution. First we briefly summarize it, in order to facilitate understanding, simplify the architecture described above, Java defines three levels of Class The 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, which includes independent security rules. So the solution is based on the three different levels of Class extension mechanisms in Java, There are three different scenarios. 4.1 Bootstrap Class Extended Scheme Java command line provides a simple way to extend the Bootstrap level class Class.-Xbootclasspath: Basic core Java class search path. Not common, otherwise you have to rewote 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. 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)

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

New Post(0)