-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCutDelegationChainClassLoader.java
51 lines (47 loc) · 1.76 KB
/
CutDelegationChainClassLoader.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
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
/**
*
*<p>
*Even though CutDelegationChainClassLoader, the class, was loaded by the VM and executed,
*when we ask this new URLClassLoader to load the very same class, it fails. Because the
*AppClassLoader (which loaded the CutDelegationChainClassLoader class the first time, since it was found on the
*CLASSPATH) isn't part of ucl's ClassLoader delegation chain, and ucl itself can't find it, then ucl gives
*up and throws a ClassNotFoundException, as it should.
*</p>
*
*/
public class CutDelegationChainClassLoader {
/**
*
* <pre>
* <code>
Exception in thread "main" <b>java.lang.ClassNotFoundException</b>: CutDelegationChainClassLoader
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at CutDelegationChainClassLoader.main(CutDelegationChainClassLoader.java:37)
</code>
</pre>
*/
public static void main(String[] args) throws Exception {
// Create a URLClassLoader that uses as its parent
// ClassLoader the bootstrap ClassLoader (indicated
// by the "null" URLClassLoader constructor's
// second argument value). This means that this
// ClassLoader no longer uses the AppClassLoader and
// ExtClassLoader as parent delegates, and should
// not pick up code along the CLASSPATH (such as the
// current directory).
//
URL[] urlArray =
{
new File("./nonexistent_directory").toURI().toURL()
};
URLClassLoader cl = new URLClassLoader(urlArray, null);
cl.loadClass(CutDelegationChainClassLoader.class.getName());
}
}