-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathManagerLoad.java
executable file
·67 lines (57 loc) · 1.88 KB
/
ManagerLoad.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class ManagerLoad {
public static void main(String[] args){
System.out.println("ManagerLoad BEGIN:");
ClassLoader loader=new FileClassLoader("c:\\CommandInformation.class");
try{
System.out.println("Try to load class");
Command command=(Command)loader.loadClass("CommandInformation").newInstance();
System.out.println("loader.loadClass is done :"+command);
System.out.println(command.getResult());
System.out.println(command.action());
System.out.println("Class is load");
}catch(Exception ex){
System.out.println("Load class: Exception:"+ex.getMessage());
ex.printStackTrace();
}
System.out.println("ManagerLoad -END-:");
}
/** return object from file or return null, when throw Exception */
private static Object getObjectFromFileName(String pathToFile){
Object returnValue=null;
try{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File(pathToFile)));
returnValue=ois.readObject();
}catch(Exception ex){
System.out.println("getObjectFromFileName: Exception:"+ex.getMessage());
}
return returnValue;
}
}
class FileClassLoader extends ClassLoader{
private String pathToFile;
public FileClassLoader(String pathToFile){
this.pathToFile=pathToFile;
}
public Class<?> findClass(String className){
try{
byte[] buffer=loadClassData(className);
return this.defineClass(className, buffer, 0, buffer.length);
}catch(Exception ex){
return null;
}
}
public byte[] loadClassData(String className){
try{
File file=new File(this.pathToFile);
byte[] buffer=new byte[(int) file.length()];
FileInputStream fis=new FileInputStream(new File(this.pathToFile));
fis.read(buffer);
return buffer;
}catch(Exception ex){
return null;
}
}
}