1 package org
.argeo
.slc
.detached
;
3 import java
.lang
.reflect
.Method
;
4 import java
.util
.ArrayList
;
6 import java
.util
.Properties
;
8 public class AppLauncher
{
9 private Properties systemProperties
= new Properties();
10 private String mainClass
= null;
11 private List arguments
= new ArrayList();
13 public void launch() {
15 Properties base
= System
.getProperties();
16 Properties fake
= new Properties(base
);
19 if (mainClass
== null)
20 throw new DetachedException(
21 "A main class name has to be specified.");
23 System
.getProperties().putAll(systemProperties
);
25 ClassLoader cl
= Thread
.currentThread().getContextClassLoader();
26 Class clss
= cl
.loadClass(mainClass
);
28 String
[] args
= new String
[arguments
.size()];
29 for (int i
= 0; i
< arguments
.size(); i
++) {
30 args
[i
] = arguments
.get(i
).toString();
33 Class
[] mainArgsClasses
= new Class
[] { args
.getClass() };
34 Object
[] mainArgs
= { args
};
35 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
37 System
.setProperties(fake
);
39 mainMethod
.invoke(null, mainArgs
);
41 } catch (Exception e
) {
42 throw new DetachedException("Unexpected exception while launching "
45 System
.setProperties(base
);
50 public void setSystemProperties(Properties systemProperties
) {
51 this.systemProperties
= systemProperties
;
54 public void setMainClass(String mainClass
) {
55 this.mainClass
= mainClass
;
58 public void setArguments(List arguments
) {
59 this.arguments
= arguments
;