List of usage examples for java.lang Process exitValue
public abstract int exitValue();
From source file:com.samczsun.helios.Helios.java
public static void relaunchAsAdmin() throws IOException, InterruptedException, URISyntaxException { File currentJarLocation = getJarLocation(); File javawLocation = getJavawLocation(); if (currentJarLocation == null) { SWTUtil.showMessage("Could not relaunch as admin - unable to find Helios.jar"); return;// ww w .j av a2s. co m } if (javawLocation == null) { SWTUtil.showMessage("Could not relaunch as admin - unable to find javaw.exe"); return; } File tempVBSFile = File.createTempFile("tmpvbs", ".vbs"); PrintWriter writer = new PrintWriter(tempVBSFile); writer.println("Set objShell = CreateObject(\"Wscript.Shell\")"); writer.println("strPath = Wscript.ScriptFullName"); writer.println("Set objFSO = CreateObject(\"Scripting.FileSystemObject\")"); writer.println("Set objFile = objFSO.GetFile(strPath)"); writer.println("strFolder = objFSO.GetParentFolderName(objFile)"); writer.println("Set UAC = CreateObject(\"Shell.Application\")"); writer.println("UAC.ShellExecute \"\"\"" + javawLocation.getAbsolutePath() + "\"\"\", \"-jar \"\"" + currentJarLocation.getAbsolutePath() + "\"\"\", strFolder, \"runas\", 1"); writer.println("WScript.Quit 0"); writer.close(); Process process = Runtime.getRuntime().exec("cscript " + tempVBSFile.getAbsolutePath()); process.waitFor(); System.exit(process.exitValue()); }
From source file:org.esa.s2tbx.dataio.openjpeg.OpenJpegUtils.java
public static CommandOutput runProcess(ProcessBuilder builder) throws InterruptedException, IOException { builder.environment().putAll(System.getenv()); StringBuilder output = new StringBuilder(); boolean isStopped = false; final Process process = builder.start(); try (BufferedReader outReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { while (!isStopped) { while (outReader.ready()) { String line = outReader.readLine(); if (line != null && !line.isEmpty()) { output.append(line); }// w ww .j a v a 2s.c om } if (!process.isAlive()) { isStopped = true; } else { Thread.yield(); } } outReader.close(); } int exitCode = process.exitValue(); //String output = convertStreamToString(process.getInputStream()); String errorOutput = convertStreamToString(process.getErrorStream()); return new CommandOutput(exitCode, output.toString(), errorOutput); }
From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java
public static void restoreMongoDbFromDump(String dumpLocation) throws IOException, InterruptedException { logger.info("restoring DB from " + dumpLocation); Process exec = Runtime.getRuntime().exec("mongorestore " + dumpLocation); exec.waitFor();//from w w w . ja va2 s . c o m String line; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream())); while ((line = bufferedReader.readLine()) != null) { logger.info("mongorestore output:" + line); } bufferedReader.close(); bufferedReader = new BufferedReader(new InputStreamReader(exec.getErrorStream())); while ((line = bufferedReader.readLine()) != null) { logger.info("mongorestore errorOutput:" + line); } bufferedReader.close(); logger.info("mongorestore exit value: " + exec.exitValue()); }
From source file:integratedtoolkit.util.OptimisComponents.java
private static boolean connectionAvailable(String resourceName, String user) { String[] command;//from w w w.j a v a 2 s. c om if (resourceName.startsWith("http://")) { command = new String[] { "/bin/sh", "-c", "wget " + resourceName }; } else { command = new String[] { "/bin/sh", "-c", "ssh " + user + "@" + resourceName + " ls" }; } System.out.println("--------------- Connection Available??----------"); System.out.println("USER:" + System.getProperty("user.name")); System.out.println("COMMAND:" + command[2]); System.out.println("--------------- Connection Available END----------"); Process p; int exitValue = -1; try { p = Runtime.getRuntime().exec(command); Thread.sleep(5000); exitValue = p.exitValue(); } catch (IllegalThreadStateException ex) { exitValue = -1; } catch (IOException ex) { exitValue = -1; } catch (InterruptedException ex) { exitValue = -1; } return (exitValue == 0); }
From source file:com.samczsun.helios.Helios.java
public static void addToContextMenu() { try {//w ww .j a v a 2 s.co m if (System.getProperty("os.name").toLowerCase().contains("win")) { Process process = Runtime.getRuntime().exec("reg add HKCR\\*\\shell\\helios\\command /f"); process.waitFor(); if (process.exitValue() == 0) { process = Runtime.getRuntime() .exec("reg add HKCR\\*\\shell\\helios /ve /d \"Open with Helios\" /f"); process.waitFor(); if (process.exitValue() == 0) { File currentJarLocation = getJarLocation(); if (currentJarLocation != null) { File javaw = getJavawLocation(); if (javaw != null) { process = Runtime.getRuntime() .exec("reg add HKCR\\*\\shell\\helios\\command /ve /d \"\\\"" + javaw.getAbsolutePath() + "\\\" -jar \\\"" + currentJarLocation.getAbsolutePath() + "\\\" \\\"%1\\\"\" /f"); process.waitFor(); if (process.exitValue() == 0) { SWTUtil.showMessage("Done"); } else { SWTUtil.showMessage("Failed to set context menu"); } } else { SWTUtil.showMessage("Could not set context menu - unable to find javaw.exe"); } } else { SWTUtil.showMessage("Could not set context menu - unable to find Helios.jar"); } } else { SWTUtil.showMessage("Failed to set context menu"); } } else { if (SWTUtil.promptForYesNo("UAC", "Helios must be run as an administrator to do this. Relaunch as administrator?")) { relaunchAsAdmin(); } } } else { SWTUtil.showMessage("You may only do this on Windows"); } } catch (Throwable t) { ExceptionHandler.handle(t); } }
From source file:ReadTemp.java
/** Executes the given applescript code and returns the first line of the output as a string */// w ww.ja v a2s .c o m static String doApplescript(String script) { String line; try { // Start applescript Process p = Runtime.getRuntime().exec("/usr/bin/osascript -s o -"); // Send applescript via stdin OutputStream stdin = p.getOutputStream(); stdin.write(script.getBytes()); stdin.flush(); stdin.close(); // get first line of output BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); line = input.readLine(); input.close(); // If we get an exit code, print it out if (p.waitFor() != 0) { System.err.println("exit value = " + p.exitValue()); System.err.println(line); } return line; } catch (Exception e) { System.err.println(e); } return ""; }
From source file:net.pms.util.ProcessUtil.java
public static void destroy(final Process p) { if (p != null) { final Integer pid = getProcessID(p); if (pid != null) { // Unix only LOGGER.trace("Killing the Unix process: " + pid); Runnable r = new Runnable() { @Override//from ww w. j a v a2 s .co m public void run() { try { Thread.sleep(TERM_TIMEOUT); } catch (InterruptedException e) { } try { p.exitValue(); } catch (IllegalThreadStateException itse) { // still running: nuke it // kill -14 (ALRM) works (for MEncoder) and is less dangerous than kill -9 // so try that first if (!kill(pid, 14)) { try { // This is a last resort, so let's not be too eager Thread.sleep(ALRM_TIMEOUT); } catch (InterruptedException ie) { } kill(pid, 9); } } } }; Thread failsafe = new Thread(r, "Process Destroyer"); failsafe.start(); } p.destroy(); } }
From source file:com.arm.connector.bridge.core.Utils.java
/** * Execute the AWS CLI/*from w w w .ja va 2 s.c om*/ * @param logger - ErrorLogger instance * @param args - arguments for the AWS CLI * @return response from CLI action */ public static String awsCLI(ErrorLogger logger, String args) { // construct the arguments String cmd = "./aws " + args; String response = null; String error = null; try { // invoke the AWS CLI Process proc = Runtime.getRuntime().exec(cmd); response = Utils.convertStreamToString(proc.getInputStream()); error = Utils.convertStreamToString(proc.getErrorStream()); // wait to completion proc.waitFor(); int status = proc.exitValue(); // DEBUG if (status != 0) { // non-zero exit status logger.warning("AWS CLI: Invoked: " + cmd); logger.warning("AWS CLI: Response: " + response); logger.warning("AWS CLI: Errors: " + error); logger.warning("AWS CLI: Exit Code: " + status); } else { // successful exit status logger.info("AWS CLI: Invoked: " + cmd); logger.info("AWS CLI: Response: " + response); logger.info("AWS CLI: Exit Code: " + status); } } catch (IOException | InterruptedException ex) { logger.warning("AWS CLI: Exception for command: " + cmd, ex); response = null; } // return the resposne return response; }
From source file:net.pms.util.ProcessUtil.java
public static String run(int[] expectedExitCodes, String... cmd) { try {//from w w w .j a va2 s .c o m ProcessBuilder pb = new ProcessBuilder(cmd); pb.redirectErrorStream(true); Process p = pb.start(); StringBuilder output; try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { String line; output = new StringBuilder(); while ((line = br.readLine()) != null) { output.append(line).append("\n"); } } p.waitFor(); boolean expected = false; if (expectedExitCodes != null) { for (int expectedCode : expectedExitCodes) { if (expectedCode == p.exitValue()) { expected = true; break; } } } if (!expected) { LOGGER.debug("Warning: command {} returned {}", Arrays.toString(cmd), p.exitValue()); } return output.toString(); } catch (Exception e) { LOGGER.error("Error running command " + Arrays.toString(cmd), e); } return ""; }
From source file:com.samsung.sjs.Compiler.java
public static int manage_c_compiler(Process clang, CompilerOptions opts) throws IOException, InterruptedException { clang.waitFor();// w w w. j a v a 2s . c om if (clang.exitValue() != 0 && !opts.debug()) { // If clang failed and we didn't already dump its stderr StringWriter w_stdout = new StringWriter(), w_stderr = new StringWriter(); IOUtils.copy(clang.getInputStream(), w_stdout, Charset.defaultCharset()); IOUtils.copy(clang.getErrorStream(), w_stderr, Charset.defaultCharset()); String compstdout = w_stdout.toString(); String compstderr = w_stderr.toString(); System.err.println("C compiler [" + opts.clangPath() + "] failed."); System.out.println("C compiler stdout:"); System.out.println(compstdout); System.err.println("C compiler stderr:"); System.err.println(compstderr); } return clang.exitValue(); }