Changeset 264 for trunk/JFCMonitor/src/de/ugoe/cs
- Timestamp:
- 11/29/11 13:55:28 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/JarLauncher.java
r263 r264 12 12 import java.util.jar.Manifest; 13 13 14 // TODO decent exception handling; currently no exception are caught!15 14 public class JarLauncher { 16 15 17 16 String workingDir = System.getProperty("user.dir") + "/"; 18 17 String jarfile; 19 18 String[] args; 20 21 String[] classPath = new String[] {};19 20 String[] classPath = new String[] {}; 22 21 String mainClassName = ""; 23 22 ClassLoader costumClassLoader; 24 23 24 public class JarLaunchException extends Exception { 25 26 private static final long serialVersionUID = 1L; 27 28 public JarLaunchException() { 29 super(); 30 } 31 32 public JarLaunchException(String string) { 33 super(string); 34 } 35 36 public JarLaunchException(Exception e) { 37 super(e); 38 } 39 40 } 41 25 42 public JarLauncher(String jarfile, String[] args) { 26 43 this.jarfile = jarfile; … … 28 45 } 29 46 30 public void exec() throws FileNotFoundException, IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InterruptedException { 31 getInfoFromJar(); 32 initClassLoader(); 33 startSUT(); 47 public void exec() { 48 try { 49 getInfoFromJar(); 50 initClassLoader(); 51 startSUT(); 52 } catch (JarLaunchException e) { 53 System.err.println("Failure to launch application."); 54 System.err.println(e.getMessage()); 55 } 34 56 } 35 36 private void getInfoFromJar() throws FileNotFoundException, IOException { 37 JarInputStream jarInputStream = new JarInputStream(new FileInputStream(workingDir+jarfile)); 57 58 private void getInfoFromJar() throws JarLaunchException { 59 JarInputStream jarInputStream; 60 try { 61 jarInputStream = new JarInputStream(new FileInputStream(workingDir 62 + jarfile)); 63 } catch (FileNotFoundException e) { 64 throw new JarLaunchException(e); 65 } catch (IOException e) { 66 throw new JarLaunchException(e); 67 } 38 68 Manifest manifest = jarInputStream.getManifest(); 39 69 mainClassName = manifest.getMainAttributes().getValue("Main-Class"); 40 String jarClassPath = manifest.getMainAttributes().getValue("Class-Path"); 70 String jarClassPath = manifest.getMainAttributes().getValue( 71 "Class-Path"); 41 72 String[] jarClassPathElements = jarClassPath.split(" "); 42 73 classPath = new String[jarClassPathElements.length]; 43 for (int i =0; i<jarClassPathElements.length; i++) {44 classPath[i] = "file:" +workingDir+jarClassPathElements[i];74 for (int i = 0; i < jarClassPathElements.length; i++) { 75 classPath[i] = "file:" + workingDir + jarClassPathElements[i]; 45 76 } 46 77 } 47 48 private void initClassLoader() throws MalformedURLException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {78 79 private void initClassLoader() throws JarLaunchException { 49 80 URLClassLoader classLoader = (URLClassLoader) ClassLoader 50 81 .getSystemClassLoader(); 51 Method method= URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); 82 Method method; 83 try { 84 method = URLClassLoader.class.getDeclaredMethod("addURL", 85 new Class[] { URL.class }); 86 } catch (SecurityException e) { 87 throw new JarLaunchException( 88 "addURL method of URLClassLoader not accessible via reflection."); 89 } catch (NoSuchMethodException e) { 90 throw new JarLaunchException( 91 "URLClassLoader does not have addURL method. Should be impossible!!"); 92 } 52 93 method.setAccessible(true); 53 54 method.invoke(classLoader, new Object[]{new URL("file:"+workingDir+jarfile)}); 55 56 for (String element : classPath) { 57 method.invoke(classLoader, new Object[]{new URL(element)}); 94 95 try { 96 method.invoke(classLoader, new Object[] { new URL("file:" 97 + workingDir + jarfile) }); 98 for (String element : classPath) { 99 method.invoke(classLoader, new Object[] { new URL(element) }); 100 } 101 } catch (IllegalArgumentException e) { 102 throw new JarLaunchException( 103 "Illegal arguments for addURL method. Should be impossible!!"); 104 } catch (MalformedURLException e) { 105 throw new JarLaunchException(e); 106 } catch (IllegalAccessException e) { 107 throw new JarLaunchException( 108 "addURL method of URLClassLoader not accessible via reflection."); 109 } catch (InvocationTargetException e) { 110 e.printStackTrace(); 58 111 } 59 112 } 60 61 private void startSUT() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InterruptedException { 62 Class<?> mainClass = Class.forName(mainClassName); 63 Method mainMethod = mainClass.getMethod("main", new Class[]{String[].class}); 64 mainMethod.invoke(null, new Object[]{args}); 113 114 private void startSUT() throws JarLaunchException { 115 Class<?> mainClass; 116 try { 117 mainClass = Class.forName(mainClassName); 118 } catch (ClassNotFoundException e) { 119 throw new JarLaunchException("Main class not found: " 120 + mainClassName); 121 } 122 Method mainMethod; 123 try { 124 mainMethod = mainClass.getMethod("main", 125 new Class[] { String[].class }); 126 } catch (SecurityException e) { 127 throw new JarLaunchException("Main method not accessible."); 128 } catch (NoSuchMethodException e) { 129 throw new JarLaunchException("Main method not found."); 130 } 131 try { 132 mainMethod.invoke(null, new Object[] { args }); 133 } catch (IllegalArgumentException e) { 134 throw new JarLaunchException( 135 "Illegal arguments for main method. Should be impossible!!"); 136 } catch (IllegalAccessException e) { 137 throw new JarLaunchException("Main method not accessible."); 138 } catch (InvocationTargetException e) { 139 throw new JarLaunchException(e); 140 } 65 141 } 66 142 }
Note: See TracChangeset
for help on using the changeset viewer.