In the jar package dynamically loaded third-party jar class problem

  Abstract: In the jar package dynamically loaded issue of third-party jar class 

  First, in the foreword of the development of the business will be developed is often labeled as the class jar packets release.    As there are many other third-party packages to provide functions set Jar, so often used these functional packages.    The following scenario is that many Java developers often encounter: in the development, debugging stage, in the CLASSPATH set up through a third party jar packet path, prepared by their normal class work.    Completion of the development, deployment, the class will develop executable jar labeled as a package, will be found through Class.forName (String classname) or ClassLoader.getSystemClassLoader (). LoadClass (String classname) to dynamically loaded existence of a third party jar included in the class will be dished out abnormal "ClassNotFoundException. 

  This problem exists in through the jar? Jar yourself.jar to run jar packs, and the class in the jar through Class.forName (String classname) to dynamically loaded when the third class. 

  Common applications, such as depending on the user's choice of configuration, dynamically loaded from different vendors JDBC Driver. 

  Second, the background knowledge 

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

  Summarized terms: It is based on the JVM sandbox (sand box) Installation of the model to provide application-level security mechanism can be customized. 

  Third, Java Virtual Machine (JVM) in order to find Class 

  3.1 Bootstrap classes 

  Java platform is the core class, for example, such as java.lang.String. Rt.jar important and the core-level class, which is from JVM Bootstrap class loader to load. Generally placed in the () jrelib directory java_home 

  3.2 Extension classes 

  Java-based extension mechanism for expansion of the core Java function modules. Serial communication module such as Java comm.jar. Placed in the general Java_home) (jrelibext directory 

  3.3 User classes 

  Developers or other third-party developers of Java package. Through the command line - classpath or - cp, or by setting the CLASSPATH environment variable to invoke. JVM through java_home placed in the () lib ools.jar users to find and call-class . common javac tools.jar is by calling for users to find the source Java compiler. on this path leads to the User class search in the order or priority-level issues. 

  3.3.1 default values: Java or call javawa the current path (.), The class is to develop the existence of the current directory 

  3.3.2 CLASSPATH environment variable settings path. CLASSPATH If it is set, will cover the value of CLASSPATH default values 

  Java 3.3.3 implementation of the command line - classpath or - cp value, if has made this one of the two command-line parameters, it would cover the value of the value of CLASSPATH environment variable 

  3.3.4-jar Options: If passed java? Jar to run an executable jar package, which will cover the jar packets above all value. In other words, the - the jar after jar with the package the highest level of priority, If the designated - jar option, of all the environmental variables and the development of command-line search path will be ignored. JVM APPClassloader will only to jar packets for the scope of the search. executable jar of the many security-related aspects of the description, reference http://java.sun.com/docs/books/tutorial/jar/ to fully understand. 

  This is the reason why packaged applications into executable jar packets, can not be used for third-party jar packets. 

  4, solutions 

  First, we summed up in brief what, in order to facilitate understanding, to simplify the structure described above, 

  Java defined three levels of class, respectively BootStrap class, Extend Class, the User class. 

  User class rules which restrict the most complex. Enforceable in the User class in the jar package contains more independent safety rules. Solutions based on Java So three different levels of class expansion mechanism, there are three different options. 

  4.1 BootStrap class expansion programme 

  Java provides a command line-level class about how to expand the bootStrap simple method. 

  - Xbootclasspath: the core Java class search path. Less frequently, or to re-write all the core Java class 

  - Xbootclasspath / a: suffix search path in the core class behind. Common. 

  - Xbootclasspath / p: prefix in the core class search path ahead. Less frequently to avoid unnecessary conflicts. 

  Grammar as follows: 

  Java? Xbootclasspath / a: / path / myclass / account.jar:-jar yourself.jar (Unix use: No. separated) 

  Java? Xbootclasspath: / d: / myclass / account.jar;-jar yourself.jar (Window;, separated) 

  4.2 extend class expansion programme 

  Java exten class Java_home deposited in the () jrelibext directory. When called Java, the class path of the expansion of the search is automatic. Search of the Federation so that a solution very simple, all want to use a third party jar packets are copied to the ext directory. 


  4.3 User class expansion programme 

  When using the - the implementation of enforceable Jar-jar, JVM Jar package directory will be set to codebase directory, all the class in this directory search began. Therefore, if the use of other third parties jar package, a more acceptable to configuration is used jar packets Manifest expansion mechanisms. steps are as follows 

  1. Will need third-party jar package, copy in the same directory where the executable jar or a subdirectory under. 

  For example: jar wrapped in d: crmluncher.jar then you can copy all packages to jar d: crm or directory d: crmlib subdirectory under. 

  2. Revised document Manifest 

  In Manifest.mf document to add the following 

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

  Class-Path is dependent on running executable jar packets words. Details of the reference http://java.sun.com/docs/books/tutorial/ext/index.html 

  In addition to prepare their own ClassLoader to dynamically loaded class, it is even more complex and advanced technology. Limited space, do not repeat them. Be interested to learn what can go to google custom classloader 

  4.4 Recommended programme 4.1, scalability, and very good 

  V. GENERAL 

  Java security mechanism with the different versions have different JDK changes would affect many core CLASS, such as Thread, so many large-scale commercial software for JDK versions are very strict. Part of the reasons for this is, which also demands the release of their preparation Application of time, regardless of size, should they be developed and tested JDK versions. 

  Based on the test paper for windows jdk 1.4.1_03. 


  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , , , ,

Releated Java Articles

Comments

Leave a Reply