forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunJava.java
165 lines (152 loc) · 6.21 KB
/
RunJava.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipException;
public class RunJava {
public static void main(String[] args) {
try {
parseArgs(args);
} catch (Throwable ex) {
System.err.println("Uncaught Java exception:");
ex.printStackTrace();
}
}
static void parseArgs(String[] args) throws Throwable {
ArrayList<String> classpath = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-jar")) {
if (i+1 >= args.length) {
System.err.println("RunJava: Missing jar name after '-jar'.");
return;
}
runJar(args[i+1], java.util.Arrays.copyOfRange(args, i+2, args.length), classpath);
return;
} else if (args[i].equals("-classpath") || args[i].equals("-cp")) {
if (i+1 >= args.length) {
System.err.println("RunJava: Missing parameter after '"+args[i]+"'");
return;
}
for (String c : expandClassPath(args[i+1])) {
classpath.add(c);
}
i++;
} else if (args[i].startsWith("-D")) {
int eq = args[i].indexOf('=');
if (eq<0) {
System.err.println("RunJava: Missing '=' in parameter '"+args[i]+"'");
return;
}
String key = args[i].substring(2, eq);
String value = args[i].substring(eq+1, args[i].length());
System.setProperty(key, value);
} else if (!args[i].startsWith("-")) {
runClass(args[i], java.util.Arrays.copyOfRange(args, i+1, args.length), classpath);
return;
} else {
System.err.println("RunJava: Unknown parameter '"+args[i]+"'");
return;
}
}
System.err.println("RunJava: No jar or class specified to run.");
}
static void runJar(String jarname, String[] args, ArrayList<String> classpath) throws Throwable {
File jarfile = new File(jarname);
try {
JarFile jar = new JarFile(jarfile);
Manifest mf = jar.getManifest();
jar.close();
String mainClass = mf.getMainAttributes().getValue("Main-Class");
if (mainClass == null) {
System.err.println(
"RunJava: No 'Main-Class' attribute in manifest of " +
jarname);
return;
}
classpath.add(jarname);
runClass(mainClass, args, classpath);
} catch (FileNotFoundException e) {
System.err.println("RunJava: File not found: " + jarname);
} catch (ZipException e) {
System.err.println("RunJava: File is not a jar: " + jarname);
}
}
static void runClass(String mainClass, String[] args, Iterable<String> classpath) throws Throwable {
setClassPath(classpath);
runMain(loadClass(mainClass), args);
}
static void runMain(Class<?> klass, String[] args) throws Throwable {
Method main = klass.getMethod("main", String[].class);
try {
main.invoke(null, new Object[] { args });
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
static void setClassPath(Iterable<String> jars) throws MalformedURLException {
ArrayList<URL> urls = new ArrayList<URL>();
for (String jar : jars) {
urls.add(new URL("file:///" + jar));
}
// If no classpath was specified, don't touch the classloader at
// all, so we just inherit the one used to run us.
if (urls.isEmpty())
return;
URL[] urlArray = urls.toArray(new URL[urls.size()]);
URLClassLoader ucl = new URLClassLoader(urlArray,
ClassLoader.getSystemClassLoader());
Thread.currentThread().setContextClassLoader(ucl);
// Also update the java.class.path property
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String jar : jars) {
if (!first) {
sb.append(":");
}
first = false;
sb.append(jar);
}
System.setProperty("java.class.path", sb.toString());
}
static Class<?> loadClass(String name) throws ClassNotFoundException {
return Thread.currentThread().getContextClassLoader().loadClass(name);
}
// Expand classpath, as given in the "-classpath" option, to a list of
// jars or directories. As in the traditional "java" command-line
// launcher, components of the class path are separated by ":", and
// we also support the traditional (but awkward) Java wildcard syntax,
// where "dir/*" adds to the classpath all jar files in the given
// directory.
static Iterable<String> expandClassPath(String classpath) {
ArrayList<String> ret = new ArrayList<String>();
for (String component : classpath.split(":")) {
if (component.endsWith("/*")) {
File dir = new File(
component.substring(0, component.length()-2));
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
String filename = file.getPath();
if (filename.endsWith(".jar")) {
ret.add(filename);
}
}
continue; // handled this path component
}
}
ret.add(component);
}
return ret;
}
}