1 | package de.ugoe.cs.eventbench.jfcmonitor;
|
---|
2 |
|
---|
3 | import java.io.FileInputStream;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.lang.reflect.InvocationTargetException;
|
---|
7 | import java.lang.reflect.Method;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.net.URLClassLoader;
|
---|
11 | import java.util.jar.JarInputStream;
|
---|
12 | import java.util.jar.Manifest;
|
---|
13 |
|
---|
14 | // TODO decent exception handling; currently no exception are caught!
|
---|
15 | public 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 | }
|
---|