Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of Custom Class Loader and dynamic loading in Java

Shulou Source: shulou.com Published: 2022-06-01 08:02:13 09月26日 Update

Editor to share with you Java custom class loader and dynamic loading example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Custom class loader

We need a custom class loader to complete any path including network file loading, this is to get the java bytecode file, that is, the compiled class file, it may be somewhere in the world.

The class loader that implements the custom first inherits the class ClassLoader. Let's take a look at the constructor code.

Public class MyClassLoad extends ClassLoader {private String rootPath; public MyClassLoad (String rootPath) {this.rootPath = rootPath;}}

The constructor simply passes in the path, that is, the folder of the class file, excluding the path to the package name

Next, override the findClass method

/ * * find the class according to name * * / @ Override protected Class findClass (String name) throws ClassNotFoundException {Class c = findLoadedClass (name); if (c = = null) {/ / the class c = findMyClass (name) has not been loaded in the memory heap; / / implement the load class} return c;}

First of all, look for it in the memory heap. If it is not loaded, you can implement it by yourself. Take a look at the findMyClass method.

/ * load the class * * @ param name * @ return * / private Class findMyClass (String name) {try {byte [] bytes = getData (name); return this.defineClass (null, bytes, 0, bytes.length); / / call the parent method to generate the concrete class} catch (Exception e) {e.printStackTrace () } return null;}

This method returns the Class class according to the byte array. To obtain the byte array according to the class file, you can use the Apache file to operate the relevant auxiliary classes. Here, the native jdk implementation is used.

Private byte [] getData (String className) {String path = rootPath + File.separatorChar + className.replace ('., File.separatorChar) + ".class"; InputStream is = null; try {is = new FileInputStream (path); ByteArrayOutputStream stream = new ByteArrayOutputStream (); byte [] buffer = new byte [2048]; int num = 0 While ((num = is.read (buffer))! =-1) {stream.write (buffer, 0, num);} return stream.toByteArray ();} catch (IOException e) {e.printStackTrace () } finally {if (is! = null) {try {is.close ();} catch (IOException e) {e.printStackTrace ();} return null;}

This simple custom class loader is about it. If you need to implement your own encryption and decryption, you can toss about it in the byte array. No more in-depth here, our goal is to hot load a piece of java code. The possible solution is to build a java template with some built-in methods, and the outside world can add some new methods, or you can call the built-in methods.

good! Start with a simple, load a piece of code into memory and execute it.

Import java.io.File;import java.io.FileWriter;import javax.tools.JavaCompiler;import javax.tools.JavaCompiler.CompilationTask;import classload.MyClassLoad;import javax.tools.JavaFileObject;import javax.tools.StandardJavaFileManager;import javax.tools.ToolProvider;public class LoadJava {public static final String javaCode = "package classload;public class HelloWorld2 {public HelloWorld2 () {System.out.println (\" HelloWorld\ ");}}" Public static void runJavaCode () throws Exception {/ / Store java String to file String fileName = "/ Users/XXXXXXX/Documents/demo/java/classload/HelloWorld2.java"; File file = new File (fileName); FileWriter fw = new FileWriter (file); fw.write (javaCode); fw.flush (); fw.close (); / / JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler () StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager (null, null, null); Iterable

Tags: Methods files memory bytes code arrays articles paths compilation dynamics examples analysis that is content almost and then not much world tasks encryption and decryption Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Shulou Tech Info Redmi OPPO Reno