source: trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/JarLauncher.java @ 263

Last change on this file since 263 was 263, checked in by sherbold, 12 years ago
  • Property svn:mime-type set to text/plain
File size: 2.7 KB
Line 
1package de.ugoe.cs.eventbench.jfcmonitor;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.lang.reflect.InvocationTargetException;
7import java.lang.reflect.Method;
8import java.net.MalformedURLException;
9import java.net.URL;
10import java.net.URLClassLoader;
11import java.util.jar.JarInputStream;
12import java.util.jar.Manifest;
13
14// TODO decent exception handling; currently no exception are caught!
15public class JarLauncher {
16       
17        String workingDir = System.getProperty("user.dir") + "/";
18        String jarfile;
19        String[] args;
20       
21        String[] classPath = new String[]{};
22        String mainClassName = "";
23        ClassLoader costumClassLoader;
24       
25        public JarLauncher(String jarfile, String[] args) {
26                this.jarfile = jarfile;
27                this.args = args;
28        }
29
30        public void exec() throws FileNotFoundException, IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InterruptedException {
31                getInfoFromJar();
32                initClassLoader();
33                startSUT();
34        }
35       
36        private void getInfoFromJar() throws FileNotFoundException, IOException {
37                JarInputStream jarInputStream = new JarInputStream(new FileInputStream(workingDir+jarfile));
38                Manifest manifest = jarInputStream.getManifest();
39                mainClassName = manifest.getMainAttributes().getValue("Main-Class");
40                String jarClassPath = manifest.getMainAttributes().getValue("Class-Path");
41                String[] jarClassPathElements = jarClassPath.split(" ");
42                classPath = new String[jarClassPathElements.length];
43                for (int i=0; i<jarClassPathElements.length ; i++) {
44                        classPath[i] = "file:"+workingDir+jarClassPathElements[i];
45                }
46        }
47       
48        private void initClassLoader() throws MalformedURLException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
49                URLClassLoader classLoader = (URLClassLoader) ClassLoader
50                                .getSystemClassLoader();
51                Method method= URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
52                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)});
58                }
59        }
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});
65        }
66}
Note: See TracBrowser for help on using the repository browser.