Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Custom Class Loader and dynamic loading in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report