1 33 34 package edu.rice.cs.util.newjvm; 35 36 import edu.rice.cs.drjava.config.FileOption; 37 import edu.rice.cs.util.swing.Utilities; 38 import edu.rice.cs.plt.io.IOUtil; 39 40 import java.io.BufferedReader ; 41 import java.io.File ; 42 import java.io.IOException ; 43 import java.io.InputStreamReader ; 44 import java.util.Iterator ; 45 import java.util.LinkedList ; 46 import java.util.Locale ; 47 48 51 public final class ExecJVM { 52 private static final String PATH_SEPARATOR = System.getProperty("path.separator"); 53 private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US); 54 55 private ExecJVM() { } 56 57 64 public static Process runJVM(String mainClass, String [] classParams, String [] classPath, String [] jvmParams, File workDir) 65 throws IOException { 66 67 final StringBuilder buf = new StringBuilder (); 68 for (int i = 0; i < classPath.length; i++) { 69 if (i != 0) buf.append(PATH_SEPARATOR); 70 71 buf.append(classPath[i]); 72 } 73 74 return runJVM(mainClass, classParams, buf.toString(), jvmParams, workDir); 75 } 76 77 85 public static Process runJVM(String mainClass, String [] classParams, String classPath, String [] jvmParams, File workDir) 86 throws IOException { 87 88 LinkedList <String > args = new LinkedList <String >(); 89 args.add("-classpath"); 90 args.add(classPath); 91 _addArray(args, jvmParams); 92 String [] jvmWithCP = args.toArray(new String [args.size()]); 93 94 return _runJVM(mainClass, classParams, jvmWithCP, workDir); 95 } 96 97 103 public static Process runJVMPropagateClassPath(String mainClass, String [] classParams, String [] jvmParams, File workDir) 104 throws IOException { 105 Iterable <File > cp = IOUtil.parsePath(System.getProperty("java.class.path")); 106 cp = IOUtil.getAbsoluteFiles(cp); 107 return runJVM(mainClass, classParams, IOUtil.pathToString(cp), jvmParams, workDir); 108 } 109 110 115 public static Process runJVMPropagateClassPath(String mainClass, String [] classParams, File workDir) 116 throws IOException { 117 return runJVMPropagateClassPath(mainClass, classParams, new String [0], workDir); 118 } 119 120 129 private static Process _runJVM(String mainClass, String [] classParams, String [] jvmParams, File workDir) throws IOException { 130 LinkedList <String > args = new LinkedList <String >(); 131 args.add(_getExecutable()); 132 _addArray(args, jvmParams); 133 args.add(mainClass); 134 _addArray(args, classParams); 135 136 String [] argArray = args.toArray(new String [args.size()]); 137 138 Process p; 140 if ((workDir != null) && (workDir != FileOption.NULL_FILE)) { 141 if (workDir.exists()) p = Runtime.getRuntime().exec(argArray, null, workDir); 143 else { 144 Utilities.showMessageBox("Working directory does not exist:\n" + workDir + 145 "\nThe setting will be ignored. Press OK to continue.", 146 "Configuration Error"); 147 p = Runtime.getRuntime().exec(argArray); 148 } 149 } 150 else { 151 p = Runtime.getRuntime().exec(argArray); 153 } 154 return p; 155 } 156 157 164 public static void ventBuffers(Process theProc, LinkedList <String > outLines, 165 LinkedList <String > errLines) throws IOException { 166 BufferedReader outBuf = new BufferedReader (new InputStreamReader (theProc.getInputStream())); 168 BufferedReader errBuf = new BufferedReader (new InputStreamReader (theProc.getErrorStream())); 169 String output; 170 171 if (outBuf.ready()) { 172 output = outBuf.readLine(); 173 174 while (output != null) { 175 outLines.add(output); 177 if (outBuf.ready()) output = outBuf.readLine(); 178 else output = null; 179 } 180 } 181 outBuf.close(); 182 183 if (errBuf.ready()) { 184 output = errBuf.readLine(); 185 while (output != null) { 186 errLines.add(output); 188 if (errBuf.ready()) { 189 output = errBuf.readLine(); 190 } 191 else { 192 output = null; 193 } 194 } 195 } 196 errBuf.close(); 197 } 198 199 207 public static void printOutput(Process theProc, String msg, String sourceName) 208 throws IOException { 209 System.out.println(msg); 211 212 LinkedList <String > outLines = new LinkedList <String >(); 213 LinkedList <String > errLines = new LinkedList <String >(); 214 215 ventBuffers(theProc, outLines, errLines); 216 217 Iterator <String > it = outLines.iterator(); 218 String output; 219 while (it.hasNext()) { 220 output = it.next(); 221 System.out.println(" [" +sourceName + " stdout]: " + output); 222 } 223 224 it = errLines.iterator(); 225 while (it.hasNext()) { 226 output = it.next(); 227 System.out.println(" [" +sourceName + " stderr]: " + output); 228 } 229 } 230 231 private static void _addArray(LinkedList <String > list, String [] array) { 232 if (array != null) { 233 for (int i = 0; i < array.length; i++) { 234 list.add(array[i]); 235 } 236 } 237 } 238 239 240 private static boolean _isDOS() { 241 return PATH_SEPARATOR.equals(";"); 242 } 243 244 private static boolean _isNetware() { 245 return OS_NAME.indexOf("netware") != -1; 246 } 247 248 252 private static String _getExecutable() { 253 if (_isNetware()) return "java"; 255 256 File executable; 257 258 String java_home = System.getProperty("java.home") + "/"; 259 260 String [] candidates = { java_home + "../bin/java", java_home + "bin/java", java_home + "java", }; 261 262 for (int i = 0; i < candidates.length; i++) { 264 String current = candidates[i]; 265 266 if (_isDOS()) { 268 executable = new File (current + "w.exe"); 269 if (! executable.exists()) executable = new File (current + ".exe"); 270 } 271 else executable = new File (current); 272 273 275 if (executable.exists()) { 276 return executable.getAbsolutePath(); 278 } 279 } 280 281 return "java"; 284 } 285 } 286 287 | Popular Tags |