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

Last change on this file since 264 was 264, checked in by sherbold, 12 years ago
  • Property svn:mime-type set to text/plain
File size: 4.2 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
14public class JarLauncher {
15
16        String workingDir = System.getProperty("user.dir") + "/";
17        String jarfile;
18        String[] args;
19
20        String[] classPath = new String[] {};
21        String mainClassName = "";
22        ClassLoader costumClassLoader;
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
42        public JarLauncher(String jarfile, String[] args) {
43                this.jarfile = jarfile;
44                this.args = args;
45        }
46
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                }
56        }
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                }
68                Manifest manifest = jarInputStream.getManifest();
69                mainClassName = manifest.getMainAttributes().getValue("Main-Class");
70                String jarClassPath = manifest.getMainAttributes().getValue(
71                                "Class-Path");
72                String[] jarClassPathElements = jarClassPath.split(" ");
73                classPath = new String[jarClassPathElements.length];
74                for (int i = 0; i < jarClassPathElements.length; i++) {
75                        classPath[i] = "file:" + workingDir + jarClassPathElements[i];
76                }
77        }
78
79        private void initClassLoader() throws JarLaunchException {
80                URLClassLoader classLoader = (URLClassLoader) ClassLoader
81                                .getSystemClassLoader();
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                }
93                method.setAccessible(true);
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();
111                }
112        }
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                }
141        }
142}
Note: See TracBrowser for help on using the repository browser.