forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassLoaderLesson.java
More file actions
37 lines (33 loc) · 1.1 KB
/
ClassLoaderLesson.java
File metadata and controls
37 lines (33 loc) · 1.1 KB
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
package security;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Created by max on 2/15/17.
*/
public class ClassLoaderLesson {
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
URL url = new URL("file://home/max/Downloads/BumperSticker.jar");
URLClassLoader pluginLoader = new URLClassLoader(new URL[] { url });
Class<?> cl = pluginLoader.loadClass("bumpersticker.BumperSticker");
Thread t = Thread.currentThread();
ClassLoader loader = t.getContextClassLoader();
}
}
class MyLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] bytes = new byte[0];
try {
bytes = Files.readAllBytes(Paths.get("fileName"));
} catch (IOException e) {
e.printStackTrace();
}
Class<?> cl = defineClass(name, bytes, 0,
bytes.length);
return cl;
}
}