author | ksrini |
Wed, 01 Oct 2008 09:04:42 -0700 | |
changeset 1329 | ce13b59a8401 |
parent 1323 | e14a3b3536cd |
child 1343 | ecc86134ee4c |
permissions | -rw-r--r-- |
1323
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
1 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
2 |
/* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
3 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
5 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
6 |
* This code is free software; you can redistribute it and/or modify it |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
7 |
* under the terms of the GNU General Public License version 2 only, as |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
8 |
* published by the Free Software Foundation. Sun designates this |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
9 |
* particular file as subject to the "Classpath" exception as provided |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
10 |
* by Sun in the LICENSE file that accompanied this code. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
11 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
12 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
14 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
15 |
* version 2 for more details (a copy is included in the LICENSE file that |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
16 |
* accompanied this code). |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
17 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
18 |
* You should have received a copy of the GNU General Public License version |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
19 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
20 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
21 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
22 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
23 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
24 |
* have any questions. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
25 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
26 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
27 |
package sun.launcher; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
28 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
29 |
/* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
30 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
31 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
32 |
* If you write code that depends on this, you do so at your own |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
33 |
* risk. This code and its internal interfaces are subject to change |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
34 |
* or deletion without notice.</b> |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
35 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
36 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
37 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
38 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
39 |
* A utility package for the java(1), javaw(1) launchers. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
40 |
* The following are helper methods that the native launcher uses |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
41 |
* to perform checks etc. using JNI, see src/share/bin/java.c |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
42 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
43 |
import java.io.File; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
44 |
import java.io.IOException; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
45 |
import java.io.PrintStream; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
46 |
import java.lang.reflect.Method; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
47 |
import java.lang.reflect.Modifier; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
48 |
import java.util.ResourceBundle; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
49 |
import java.text.MessageFormat; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
50 |
import java.util.jar.Attributes; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
51 |
import java.util.jar.JarFile; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
52 |
import java.util.jar.Manifest; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
53 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
54 |
public enum LauncherHelper { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
55 |
INSTANCE; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
56 |
private static final String defaultBundleName = |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
57 |
"sun.launcher.resources.launcher"; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
58 |
private static ResourceBundle javarb = |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
59 |
ResourceBundle.getBundle(defaultBundleName); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
60 |
private static final String MAIN_CLASS = "Main-Class"; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
61 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
62 |
private static StringBuilder outBuf = new StringBuilder(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
63 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
64 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
65 |
* A private helper method to get a localized message and also |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
66 |
* apply any arguments that we might pass. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
67 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
68 |
private static String getLocalizedMessage(String key, Object... args) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
69 |
String msg = javarb.getString(key); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
70 |
return (args != null) ? MessageFormat.format(msg, args) : msg; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
71 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
72 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
73 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
74 |
* The java -help message is split into 3 parts, an invariant, followed |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
75 |
* by a set of platform dependent variant messages, finally an invariant |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
76 |
* set of lines. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
77 |
* This method initializes the help message for the first time, and also |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
78 |
* assembles the invariant header part of the message. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
79 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
80 |
static void initHelpMessage(String progname) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
81 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
82 |
(progname == null) ? "java" : progname )); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
83 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
84 |
32)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
85 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
86 |
64)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
87 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
88 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
89 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
90 |
* Appends the vm selection messages to the header, already created. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
91 |
* initHelpSystem must already be called. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
92 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
93 |
static void appendVmSelectMessage(String vm1, String vm2) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
94 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
95 |
vm1, vm2)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
96 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
97 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
98 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
99 |
* Appends the vm synoym message to the header, already created. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
100 |
* initHelpSystem must be called before using this method. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
101 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
102 |
static void appendVmSynonymMessage(String vm1, String vm2) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
103 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
104 |
vm1, vm2)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
105 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
106 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
107 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
108 |
* Appends the vm Ergo message to the header, already created. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
109 |
* initHelpSystem must be called before using this method. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
110 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
111 |
static void appendVmErgoMessage(boolean isServerClass, String vm) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
112 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.ergo.message1", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
113 |
vm)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
114 |
outBuf = (isServerClass) |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
115 |
? outBuf.append(",\n" + |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
116 |
getLocalizedMessage("java.launcher.ergo.message2") + "\n\n") |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
117 |
: outBuf.append(".\n\n"); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
118 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
119 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
120 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
121 |
* Appends the last invariant part to the previously created messages, |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
122 |
* and finishes up the printing to the desired output stream. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
123 |
* initHelpSystem must be called before using this method. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
124 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
125 |
static void printHelpMessage(boolean printToStderr) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
126 |
PrintStream ostream = (printToStderr) ? System.err : System.out; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
127 |
outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
128 |
File.pathSeparator)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
129 |
ostream.println(outBuf.toString()); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
130 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
131 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
132 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
133 |
* Prints the Xusage text to the desired output stream. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
134 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
135 |
static void printXUsageMessage(boolean printToStderr) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
136 |
PrintStream ostream = (printToStderr) ? System.err : System.out; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
137 |
ostream.println(getLocalizedMessage("java.launcher.X.usage", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
138 |
File.pathSeparator)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
139 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
140 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
141 |
static String getMainClassFromJar(String jarname) throws IOException { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
142 |
JarFile jarFile = null; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
143 |
try { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
144 |
jarFile = new JarFile(jarname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
145 |
Manifest manifest = jarFile.getManifest(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
146 |
if (manifest == null) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
147 |
throw new IOException("manifest not found in " + jarname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
148 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
149 |
Attributes mainAttrs = manifest.getMainAttributes(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
150 |
if (mainAttrs == null) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
151 |
throw new IOException("no main mainifest attributes, in " + |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
152 |
jarname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
153 |
} |
1329
ce13b59a8401
4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents:
1323
diff
changeset
|
154 |
return mainAttrs.getValue(MAIN_CLASS).trim(); |
1323
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
155 |
} finally { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
156 |
if (jarFile != null) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
157 |
jarFile.close(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
158 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
159 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
160 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
161 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
162 |
/** |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
163 |
* This method does the following: |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
164 |
* 1. gets the classname from a Jar's manifest, if necessary |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
165 |
* 2. loads the class using the System ClassLoader |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
166 |
* 3. ensures the availability and accessibility of the main method, |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
167 |
* using signatureDiagnostic method. |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
168 |
* a. does the class exist |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
169 |
* b. is there a main |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
170 |
* c. is the main public |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
171 |
* d. is the main static |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
172 |
* c. does the main take a String array for args |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
173 |
* 4. and off we go...... |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
174 |
* |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
175 |
* @param printToStderr |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
176 |
* @param isJar |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
177 |
* @param name |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
178 |
* @return |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
179 |
* @throws java.lang.Exception |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
180 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
181 |
public static Object checkAndLoadMain(boolean printToStderr, |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
182 |
boolean isJar, String name) throws Exception { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
183 |
// get the class name |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
184 |
String classname = (isJar) ? getMainClassFromJar(name) : name; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
185 |
classname = classname.replace('/', '.'); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
186 |
ClassLoader loader = ClassLoader.getSystemClassLoader(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
187 |
Class<?> clazz = null; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
188 |
PrintStream ostream = (printToStderr) ? System.err : System.out; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
189 |
try { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
190 |
clazz = loader.loadClass(classname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
191 |
} catch (ClassNotFoundException cnfe) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
192 |
ostream.println(getLocalizedMessage("java.launcher.cls.error1", classname)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
193 |
throw new RuntimeException("Could not find the main class " + classname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
194 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
195 |
signatureDiagnostic(ostream, clazz); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
196 |
return clazz; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
197 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
198 |
|
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
199 |
static void signatureDiagnostic(PrintStream ostream, Class<?> clazz) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
200 |
String classname = clazz.getName(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
201 |
Method method = null; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
202 |
try { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
203 |
method = clazz.getMethod("main", String[].class); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
204 |
} catch (Exception e) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
205 |
ostream.println(getLocalizedMessage("java.launcher.cls.error4", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
206 |
classname)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
207 |
throw new RuntimeException("Main method not found in " + classname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
208 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
209 |
/* |
1329
ce13b59a8401
4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents:
1323
diff
changeset
|
210 |
* getMethod (above) will choose the correct method, based |
ce13b59a8401
4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents:
1323
diff
changeset
|
211 |
* on its name and parameter type, however, we still have to |
ce13b59a8401
4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents:
1323
diff
changeset
|
212 |
* ensure that the method is static and returns a void. |
1323
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
213 |
*/ |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
214 |
int mod = method.getModifiers(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
215 |
if (!Modifier.isStatic(mod)) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
216 |
ostream.println(getLocalizedMessage("java.launcher.cls.error2", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
217 |
"static", classname)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
218 |
throw new RuntimeException("Main method is not static in class " + |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
219 |
classname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
220 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
221 |
Class<?> rType = method.getReturnType(); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
222 |
if (!rType.isPrimitive() || !rType.getName().equals("void")) { |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
223 |
ostream.println(getLocalizedMessage("java.launcher.cls.error3", |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
224 |
classname)); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
225 |
throw new RuntimeException("Main method must return a value" + |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
226 |
" of type void in class " + |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
227 |
classname); |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
228 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
229 |
return; |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
230 |
} |
e14a3b3536cd
6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff
changeset
|
231 |
} |