Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Networking Code Examples

Multi ClassLoader Demo

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
Multi ClassLoader Demo import java.net.URLClassLoader; import java.net.URL; import java.io.File; public class MultiCLDemo { public static void main(String[] args) { if (args.length<3) { System.out.println("Usage: java MultiCLDemo "+" ..."); System.out.println("Loads and runs toString() on class_name "+"found in each subDir."); System.exit(0); } try { String className = args[0]; int count = args.length-1; URL tmpURL; Object[] objects = new Object[count]; URLClassLoader[] loaders = new URLClassLoader[count]; System.out.println("Loading "+count+" different classes "+"named: "+className); for (int i=0; i { tmpURL = new File(args[i+1]).toURL(); System.out.println("\nLoading from: "+tmpURL); loaders[i] = new URLClassLoader(new URL[] { tmpURL }); objects[i] = loaders[i].loadClass(className).newInstance(); for (int k=0; k<=i; k++) { System.out.println("Got object: "+objects[k]); } } } catch (Exception e) { e.printStackTrace(); } } } /*Some sample classes to load... // c:\a/HelloWorld.java public class HelloWorld { public String toString() { return "Hello"; } } // c:\b/HelloWorld.java public class HelloWorld { public String toString() { return "World"; } } // c:\c/HelloWorld.java public class HelloWorld { public String toString() { return "Hello World"; } } The code running... java MultiCLDemo HelloWorld a b c Loading 3 different classes named: HelloWorldLoading from: file:/.../class_loader_demo/a/Got object: HelloLoading from: file:/.../class_loader_demo/b/Got object: HelloGot object: WorldLoading from: file:/.../class_loader_demo/c/Got object: HelloGot object: WorldGot object: Hello World regards, btcoburn*/