For
another thread I created an example ClassLoader to demonstrate how you can have two different instances of a static field running in the same JVM. There is a zip file with code at:
http://flick.nerdc.ufl.edu/jgo/ClassLoaderNamespaces.zip but incase the server is offline I'll include the code below.
File: /game/foo/Bar.java
1 2 3 4 5 6 7 8 9
| package foo;
public class Bar { private static int count = 0;
public String toString() { return String.valueOf(++count); } } |
File: /loader/Test.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
| import java.io.File;
public class Test { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { String myPackage = "foo."; String baseDir = ".." + File.separator + "game" + File.separator; ClassLoader cl1 = new SimpleClassLoader(myPackage, baseDir); ClassLoader cl2 = new SimpleClassLoader(myPackage, baseDir);
Object o1 = cl1.loadClass("foo.Bar").newInstance(); Object o2 = cl2.loadClass("foo.Bar").newInstance(); Object o3 = cl1.loadClass("foo.Bar").newInstance(); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("======="); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("======="); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("======="); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("o3: " + o3.toString()); System.out.println("======="); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("o3: " + o3.toString()); System.out.println("======="); System.out.println("o1: " + o1.toString()); System.out.println("o2: " + o2.toString()); System.out.println("o3: " + o3.toString()); } } |
File: /loader/SimpleClassLoader.java which is based on:
http://www.javaworld.com/javaworld/jw-10-1996/indepth/indepth.src.html1 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
import java.io.FileInputStream; import java.io.File; import java.util.Hashtable;
public class SimpleClassLoader extends ClassLoader { private Hashtable classes = new Hashtable();
private String myPackage; private String codeBase;
public SimpleClassLoader(String myPackage, String codeBase) { this.myPackage = myPackage; this.codeBase = codeBase; }
private byte getClassImplFromDataBase(String className)[] { System.err.println(" >>>>>> Fetching the implementation of " + className); byte result[];
StringBuffer sb = new StringBuffer(className); for (int i=0, n=sb.length(); i < n; i++) { if (sb.charAt(i) == '.') { sb.setCharAt(i, File.separatorChar); } } className = sb.append(".class").toString(); System.err.println("New className: " + className); try { FileInputStream fi = new FileInputStream(codeBase + className); result = new byte[fi.available()]; fi.read(result); return result; } catch (Exception e) {
return null; } }
public Class loadClass(String className) throws ClassNotFoundException { return (loadClass(className, true)); }
public synchronized Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException { Class result; byte classData[];
System.err.println(" >>>>>> Load class : " + className);
result = (Class)classes.get(className); if (result != null) { System.err.println(" >>>>>> returning cached result."); return result; }
if (!className.startsWith(myPackage)) { try { result = super.findSystemClass(className); System.err.println(" >>>>>> returning system class (in CLASSPATH)."); return result; } catch (ClassNotFoundException e) { System.err.println(" >>>>>> Not a system class."); } }
classData = getClassImplFromDataBase(className); if (classData == null) { throw new ClassNotFoundException(); }
result = defineClass(classData, 0, classData.length); if (result == null) { throw new ClassFormatError(); }
if (resolveIt) { resolveClass(result); }
classes.put(className, result); System.err.println(" >>>>>> Returning newly loaded class."); return result; } }
|
And when you run `java Test` you get output something like:
o1: 1
o2: 1
=======
o1: 2
o2: 2
=======
o1: 3
o2: 3
=======
o1: 4
o2: 4
o3: 5
=======
o1: 6
o2: 5
o3: 7
=======
o1: 8
o2: 6
o3: 9
Hope this is useful.