-
Notifications
You must be signed in to change notification settings - Fork 1
/
StaticBoundaryWithClassLoader.java
63 lines (58 loc) · 1.71 KB
/
StaticBoundaryWithClassLoader.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
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
/**
* What is static in java?
* <p>
* Do you really know singleton in java?
*
* <p>
* This sample is based on SUN JVM.
*/
public class StaticBoundaryWithClassLoader {
private static int instanceCount = 0;
public StaticBoundaryWithClassLoader() {
System.out.println("This is instance #" + ++instanceCount);
}
/**
*
* NOTE:<br>
* it may NOT work in your IDE, if your IDE doesn't integrate "user.dir" into it.<br>
* otherwise you can hardcode StaticBoundaryWithClassLoader class path to make it work.
<pre>
<code>
C:\Users\yuguo\Crater\PluginWs\src>java StaticBoundaryWithClassLoader
This is instance #1
This is instance #1
</code>
</pre>
*/
public static void main(String args[]) throws Exception {
new StaticBoundaryWithClassLoader();
// 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(System.getProperty("user.dir")).toURI().toURL() };
URLClassLoader cl = new URLClassLoader(urlArray, null);
cl.loadClass("StaticBoundaryWithClassLoader").newInstance();
}
class Helper{
/**
* helper to show SYSTEM Environment<br>
* two properties concern us here.<br>
* <li>
* user.dir: process current working directory
* <li>
* sun.boot.class.path: bootstrap path
*/
public void showEnvProperties(){
System.getProperties().list(System.out);
}
}
}