/** * System command. Execute a command and insert the result. * * @param args * @param help * @param patterns * @param low * @param high */ public String system_internal(boolean allowFail, String args[]) throws Exception { verifyCommand( args, "${" + (allowFail ? "system-allow-fail" : "system") + ";<command>[;<in>]}, execute a system command", null, 2, 3); String command = args[1]; String input = null; if (args.length > 2) { input = args[2]; } Process process = Runtime.getRuntime().exec(command, null, domain.getBase()); if (input != null) { process.getOutputStream().write(input.getBytes("UTF-8")); } process.getOutputStream().close(); String s = IO.collect(process.getInputStream(), "UTF-8"); int exitValue = process.waitFor(); if (exitValue != 0) return exitValue + ""; if (!allowFail && (exitValue != 0)) { domain.error("System command " + command + " failed with " + exitValue); } return s.trim(); }
// Creates a new thread, runs the program in that thread, and reports any errors as needed. private void run(String clazz) { try { // Makes sure the JVM resets if it's already running. if (JVMrunning) kill(); // Some String constants for java path and OS-specific separators. String separator = System.getProperty("file.separator"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; // Tries to run compiled code. ProcessBuilder builder = new ProcessBuilder(path, clazz); // Should be good now! Everything past this is on you. Don't mess it up. println( "Build succeeded on " + java.util.Calendar.getInstance().getTime().toString(), progErr); println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", progErr); JVM = builder.start(); // Note that as of right now, there is no support for input. Only output. Reader errorReader = new InputStreamReader(JVM.getErrorStream()); Reader outReader = new InputStreamReader(JVM.getInputStream()); // Writer inReader = new OutputStreamWriter(JVM.getOutputStream()); redirectErr = redirectIOStream(errorReader, err); redirectOut = redirectIOStream(outReader, out); // redirectIn = redirectIOStream(null, inReader); } catch (Exception e) { // This catches any other errors we might get. println("Some error thrown", progErr); logError(e.toString()); displayLog(); return; } }
public static int ensureNTServiceIsRunning(String serviceName, String service) throws Exception { ArrayList<String> serviceList = getRunningNTServices(); if (serviceList == null) return -1; // not NT kernel? for (String svc : serviceList) { if (serviceName.equals(svc.trim())) return 0; // service already running } Process process = RUNTIME.exec(comSpec + "net start \"" + service + "\""); process.waitFor(); // wait for the process to complete int rc = process.exitValue(); // pick up its return code boolean success = (rc == 0); if (success) System.out.println("Successfully started service '" + serviceName + "'."); return (success ? 1 : -1); }
public static boolean exec(String cmd) throws Exception { int exitVal = -1; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(new String[] {"/bin/bash", "-c", cmd}); OutputHandler err = new OutputHandler(proc.getErrorStream(), cmd); err.start(); OutputHandler out = new OutputHandler(proc.getInputStream(), cmd); out.start(); exitVal = proc.waitFor(); } catch (Throwable t) { t.printStackTrace(); } return (exitVal == 0); }
// Kills the JVM process and any active threads on it. private void kill() { if (redirectErr != null) { redirectErr.close(); redirectErr.interrupt(); } if (redirectOut != null) { redirectOut.close(); redirectOut.interrupt(); } if (JVM != null) { JVM.destroy(); JVM = null; } JVMrunning = false; }
// Kills the JVM process and any active threads on it. private void kill() { if (redirectErr != null) { redirectErr.close(); redirectErr.interrupt(); } if (redirectOut != null) { redirectOut.close(); redirectOut.interrupt(); } if (JVM != null) { JVM.destroy(); JVM = null; } JVMrunning = false; println("JVM reset on " + java.util.Calendar.getInstance().getTime().toString(), progErr); }
/** * Given class of the interface for a fluid engine, build a concrete instance of that interface * that calls back into a Basic Engine for all methods in the interface. */ private static BasicEngine buildCallbackEngine(String engineInterface, Class cEngine) { // build up list of callbacks for <engineInterface> StringBuffer decls = new StringBuffer(); Method[] engineMethods = cEngine.getMethods(); for (int i = 0; i < engineMethods.length; i++) { Method meth = engineMethods[i]; // skip methods that aren't declared in the interface if (meth.getDeclaringClass() != cEngine) { continue; } decls.append( " public " + Utils.asTypeDecl(meth.getReturnType()) + " " + meth.getName() + "("); Class[] params = meth.getParameterTypes(); for (int j = 0; j < params.length; j++) { decls.append(Utils.asTypeDecl(params[j]) + " p" + j); if (j != params.length - 1) { decls.append(", "); } } decls.append(") {\n"); decls.append( " return (" + Utils.asTypeDecl(meth.getReturnType()) + ")super.callNative(\"" + meth.getName() + "\", new Object[] {"); for (int j = 0; j < params.length; j++) { decls.append("p" + j); if (j != params.length - 1) { decls.append(", "); } } decls.append("} );\n"); decls.append(" }\n\n"); } // write template file String engineName = "BasicEngine_" + engineInterface; String fileName = engineName + ".java"; try { String contents = Utils.readFile("lava/engine/basic/BasicEngineTemplate.java").toString(); // remove package name contents = Utils.replaceAll(contents, "package lava.engine.basic;", ""); // for class decl contents = Utils.replaceAll( contents, "extends BasicEngine", "extends BasicEngine implements " + engineInterface); // for constructor contents = Utils.replaceAll(contents, "BasicEngineTemplate", engineName); // for methods contents = Utils.replaceAll(contents, "// INSERT METHODS HERE", decls.toString()); Utils.writeFile(fileName, contents); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // compile the file try { Process jProcess = Runtime.getRuntime().exec("jikes " + fileName, null, null); jProcess.waitFor(); } catch (Exception e) { System.err.println("Error compiling auto-generated file " + fileName); e.printStackTrace(); System.exit(1); } // instantiate the class BasicEngine result = null; try { result = (BasicEngine) Class.forName(engineName).newInstance(); // cleanup the files we made new File(engineName + ".java").delete(); new File(engineName + ".class").delete(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return result; }