Go from: http://blog.csdn.net/mousebaby808/article/details/31788325 an overview of servers such as Tomcat, The jar file under the Lib directory in the application and the class file in the classes directory will be loaded at startup, as well as the framework of spring, and the specified class file can be scanned and loaded according to the specified path, this technology can implement a container to accommodate various sub-applications. Java class because of the need to load and compile bytecode, dynamic loading class file is more troublesome, unlike the C-load dynamic link library as long as a file name can be done, but the JDK still provides a complete set of methods to dynamically load jar files and class files. Dynamically Loading JAR files
[Java]View PlainCopy
- System Class Library Path
- File LibPath = new file (the path where the jar file is located);
- Get all. jar and. zip files
- file[] Jarfiles = libpath.listfiles (new FilenameFilter () {
- Public Boolean Accept (File dir, String name) {
- return Name.endswith (". Jar") | | name.endswith (". zip");
- }
- });
- if (jarfiles! = null) {
- //How to get the folder of the class from the URLClassLoader class
- //For jar files, it can be understood as a folder for storing class files.
- method = URLClassLoader. Class.getdeclaredmethod ("Addurl", URL. Class);
- Boolean accessible = Method.isaccessible (); //Get access to methods
- try {
- if (accessible = = false) {
- Method.setaccessible (true); //Set access permissions for the method
- }
- //Get System class loader
- URLClassLoader ClassLoader = (urlclassloader) classloader.getsystemclassloader ();
- For (File file:jarfiles) {
- URL url = File.touri (). Tourl ();
- try {
- Method.invoke (ClassLoader, URL);
- Log.debug ("read jar file [name={}]", File.getname ());
- } catch (Exception e) {
- Log.error ("read jar file [name={}] Failed", File.getname ());
- }
- }
- } finally {
- Method.setaccessible (accessible);
- }
- }
Dynamically Loading class files
[Java]View PlainCopy
- Set the root path of the class file
- For example, there is a test under/usr/java/classes. App class, The/usr/java/classes is the root path of this class, and the actual location of the. class file is/usr/java/classes/test/app.class
- File Clazzpath = new File (theroot path where the class file is located);
- Record the number of. class files Loaded
- int clazzcount = 0;
- if (clazzpath.exists () && clazzpath.isdirectory ()) {
- //Get path length
- int clazzpathlen = Clazzpath.getabsolutepath (). Length () + 1;
- stack<file> stack = new stack<> ();
- Stack.push (Clazzpath);
- //Traverse classpath
- While (stack.isempty () = = false) {
- File path = Stack.pop ();
- file[] Classfiles = path.listfiles (new FileFilter () {
- Public Boolean accept (File pathname) {
- return pathname.isdirectory () | | pathname.getname (). EndsWith (". Class");
- }
- });
- For (File subfile:classfiles) {
- if (subfile.isdirectory ()) {
- Stack.push (Subfile);
- } Else {
- if (clazzcount++ = = 0) {
- method = URLClassLoader. Class.getdeclaredmethod ("Addurl", URL. Class);
- Boolean accessible = Method.isaccessible ();
- try {
- if (accessible = = false) {
- Method.setaccessible (true);
- }
- //Set class loader
- URLClassLoader ClassLoader = (urlclassloader) classloader.getsystemclassloader ();
- //Add the current classpath to the class loader
- Method.invoke (ClassLoader, Clazzpath.touri (). Tourl ());
- } finally {
- Method.setaccessible (accessible);
- }
- }
- //File name
- String className = Subfile.getabsolutepath ();
- ClassName = classname.substring (Clazzpathlen, Classname.length ()- 6);
- ClassName = Classname.replace (File.separatorchar, '. ');
- //Load class class
- Class.forName (ClassName);
- Log.debug ("Read application class file [class={}]", className);
- }
- }
- }
- }
Once you have completed these two steps, you can use Class.forName to load the Java classes contained in the jar or. class file.
Dynamically loading jar files and class files in Java_java