Write a Web server by yourself and find that class. forname cannot be used and the class cannot be found because the class path cannot be found,
YesLoad the file path of the required class, and then use classloader to load the required class. The original
I will not say much about it.Code:
URL url = new URL ("Path of the folder where the class is located");
String classname = "Name of the class to be loaded ";
Classloader Cl = new urlclassloader (new URL [] {URL });
Class D = Cl. loadclass (classname );
// Instantiate the loaded class
Object action = Class. forname (classname). newinstance ();
Method method =Action. Getclass (). getmethod ("classname class method", parameter type );
Method. Invoke (Action, Parameter );
The following is the code snippet:
URL url = new URL ("file: \ D: \ Users \ Administrator \ workspaces \ myeclipse 8.6 \ SRC "); string name = "visit"; classloader Cl = new urlclassloader (new URL [] {URL}); Class classname = Cl. loadclass (name); system. out. println ("d ------------------" + classname); system. out. println ("Cl ----------------------" + Cl); Action = Class. forname ("visit "). newinstance (); Method method = action. getclass (). getmethod ("Showa"); method. invoke (action );