Home > Back-end >  Questions about the custom class loader
Questions about the custom class loader

Time:05-09

 
Public class CustomClassLoader extends this {

@ Override
Protected Class<?> FindClass (String name) throws ClassNotFoundException {
System. The out. Println (getSystemClassLoader ());
String path=this. GetClass (). GetResource (" "). GetPath ();
The File File=new File (path. The substring (0, the path indexOf (" class ") + 8) + name. The replaceAll (" \ \ ", "/") + ". The class ");
Try {
ByteArrayOutputStream ByteArrayOutputStream=new ByteArrayOutputStream ();
InputStreamReader fileInputStream=new InputStreamReader (new fileInputStream (file), StandardCharsets. ISO_8859_1);
//a FileInputStream FileInputStream=new FileInputStream (file);
Int b=0;
While ((b=a fileInputStream. Read ())!=0) {
ByteArrayOutputStream. Write (b);
}
ByteArrayOutputStream. Close ();
FileInputStream. Close ();
Byte [] bytes=byteArrayOutputStream. ToByteArray ();
String s=new String (bytes);
System.out.println(s);
Return super. DefineClass (name, bytes, 0, bytes. The length).
} the catch (Exception e) {
e.printStackTrace();
}
Return super. FindClass (name);
}

Public static void main (String [] args) {
CustomClassLoader CustomClassLoader=new CustomClassLoader ();
Try {
CustomClassLoader. LoadClass (" com. Me. The zookeeper. Test. CustomClassLoader ");
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
}
}
}

When running the code customClassLoader. LoadClass (" com. Me. The zookeeper. Test. CustomClassLoader "), no call custom findClass () method

CodePudding user response:

Class loader to load this class?

CodePudding user response:

https://blog.csdn.net/weixin_30342639/article/details/105389653, the nature of the class loader is initialized the JVM when loading the references to classes, methods to stack memory, heap memory, and stack method, such as you wrote a xxxUtil class, you don't want AppClassLoader load it, but through a custom class loaders loading xxxUtil, then the need to define the class loader,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
And your written above is loaded himself, it must be through the parent class loader loads, but also in the same class, source CustomClassLoader. In Java, not into the custom of the loader, CustomClassLoader. LoadClass (" com. Me. The zookeeper. Test. CustomClassLoader "); The loadClass is to load the others

CodePudding user response:

2 floor have a reference to raw response:
https://blog.csdn.net/weixin_30342639/article/details/105389653, class loader is the nature of the JVM initialization time, loading the references to classes, methods to stack memory, heap memory, and stack method, such as you wrote a xxxUtil class, you don't want AppClassLoader load it, but through a custom class loaders loading xxxUtil, then the need to define the class loader,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
And your written above is loaded himself, it must be through the parent class loader loads, but also in the same class, source CustomClassLoader. In Java, not into the custom of the loader, CustomClassLoader. LoadClass (" com. Me. The zookeeper. Test. CustomClassLoader "); The loadClass is loading others

I changed a class as if still won't do
Class<?> AClass=customClassLoader. LoadClass (" com. Me. The zookeeper. Test. The test ");

CodePudding user response:

After the project finished packing, you take the Test. The Java this file from the hard disk directory for com/me/zookeeper/Test go down

CodePudding user response:

used one to 5 floor response:
after the project finished packing, you take the Test. The Java this file from the hard disk directory for com/me/zookeeper/Test go down

I changed a way, as if still won't do
 
Public class CustomClassLoader extends this {

Public void test () {
System. The out. Println (" the test load to be achieved ");
}

@ Override
Protected Class<?> FindClass (String name) throws ClassNotFoundException {
System. The out. Println (getSystemClassLoader ());
String path=this. GetClass (). GetResource (" "). GetPath ();
The File File=new File (name);
Try {
ByteArrayOutputStream ByteArrayOutputStream=new ByteArrayOutputStream ();
FileInputStream FileInputStream=new FileInputStream (file);
Int b=0;
While ((b=a fileInputStream. Read ()) & gt; 1) {
ByteArrayOutputStream. Write (b);
}
ByteArrayOutputStream. Close ();
FileInputStream. Close ();
Byte [] bytes=byteArrayOutputStream. ToByteArray ();
String s=new String (bytes);
System.out.println(s);
Return super. DefineClass (name, bytes, 0, bytes. The length).
} the catch (Exception e) {
e.printStackTrace();
}
Return super. FindClass (name);
}

Public static void main (String [] args) {
CustomClassLoader CustomClassLoader=new CustomClassLoader ();
Try {
Class<?> AClass=customClassLoader. LoadClass (" D: \ \ wok2 \ \ zookeeper - test - the main \ \ target \ \ classes \ \ com \ \ me \ \ zookeeper \ \ test \ \ test class ");
System. The out. Println (aClass. GetClassLoader ());
System. The out. Println (aClass. GetName ());
The Test customClassLoader1=(Test) aClass. NewInstance ();
CustomClassLoader1. ClassLoaderRange ();

} the catch (a ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

CodePudding user response:

This should not, you hit a breakpoint
  • Related