1 17 package org.apache.bcel.util; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 22 38 public class JavaWrapper { 39 40 private java.lang.ClassLoader loader; 41 42 43 private static java.lang.ClassLoader getClassLoader() { 44 String s = System.getProperty("bcel.classloader"); 45 if ((s == null) || "".equals(s)) { 46 s = "org.apache.bcel.util.ClassLoader"; 47 } 48 try { 49 return (java.lang.ClassLoader ) Class.forName(s).newInstance(); 50 } catch (Exception e) { 51 throw new RuntimeException (e.toString()); 52 } 53 } 54 55 56 public JavaWrapper(java.lang.ClassLoader loader) { 57 this.loader = loader; 58 } 59 60 61 public JavaWrapper() { 62 this(getClassLoader()); 63 } 64 65 66 71 public void runMain( String class_name, String [] argv ) throws ClassNotFoundException { 72 Class cl = loader.loadClass(class_name); 73 Method method = null; 74 try { 75 method = cl.getMethod("main", new Class [] { 76 argv.getClass() 77 }); 78 80 int m = method.getModifiers(); 81 Class r = method.getReturnType(); 82 if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m) 83 || (r != Void.TYPE)) { 84 throw new NoSuchMethodException (); 85 } 86 } catch (NoSuchMethodException no) { 87 System.out.println("In class " + class_name 88 + ": public static void main(String[] argv) is not defined"); 89 return; 90 } 91 try { 92 method.invoke(null, new Object [] { 93 argv 94 }); 95 } catch (Exception ex) { 96 ex.printStackTrace(); 97 } 98 } 99 100 101 104 public static void main( String [] argv ) throws Exception { 105 107 if (argv.length == 0) { 108 System.out.println("Missing class name."); 109 return; 110 } 111 String class_name = argv[0]; 112 String [] new_argv = new String [argv.length - 1]; 113 System.arraycopy(argv, 1, new_argv, 0, new_argv.length); 114 JavaWrapper wrapper = new JavaWrapper(); 115 wrapper.runMain(class_name, new_argv); 116 } 117 } 118 | Popular Tags |