public Process createProcess() throws ExecutionException { if (LOG.isDebugEnabled()) { LOG.debug("Executing [" + getCommandLineString() + "]"); } List<String> commands; try { checkWorkingDirectory(); if (StringUtil.isEmptyOrSpaces(myExePath)) { throw new ExecutionException( IdeBundle.message("run.configuration.error.executable.not.specified")); } commands = CommandLineUtil.toCommandLine(myExePath, myProgramParams.getList()); } catch (ExecutionException e) { LOG.warn(e); throw e; } try { ProcessBuilder builder = new ProcessBuilder(commands); setupEnvironment(builder.environment()); builder.directory(myWorkDirectory); builder.redirectErrorStream(myRedirectErrorStream); return builder.start(); } catch (IOException e) { LOG.warn(e); throw new ProcessNotCreatedException(e.getMessage(), e, this); } }
public static String test123() { String r = ""; try { // System.setSecurityManager(null); // String[] callArgs = {"mkdir", "/home/alexis/test123"}; String[] callArgs = {"ls"}; ProcessBuilder pb = new ProcessBuilder(callArgs); pb.redirectErrorStream(true); Process p = pb.start(); p.waitFor(); // BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream() )); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; System.out.println("Start Output"); while ((line = br.readLine()) != null) r += line + "\n"; System.out.println("End Output"); /*Process p = Runtime.getRuntime().exec(callArgs); p.waitFor(); System.out.println("Test Complete");*/ r = line; } catch (IOException e) { System.out.println(e); r = "Test failed"; } catch (Exception e) { } return r; }
/** Run new Maven os process with given arguments and commands. */ public void execute() { Process process = null; BufferedReader br = null; try { ProcessBuilder processBuilder = getProcessBuilder(); processBuilder.redirectErrorStream(true); log.info("Starting process: " + processBuilder.command()); process = processBuilder.start(); // Read output br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = br.readLine()) != null) { System.out.println(line); } process.waitFor(); } catch (Exception e) { throw new RuntimeException(e); } finally { close(br); destroyProcess(process); } }
private void startProcess() throws IOException, SrvrMgrException { ProcessBuilder builder = new ProcessBuilder(runTimeCommand); this.lastCommandDate = System.currentTimeMillis(); try { builder.redirectErrorStream(true); this.process = builder.start(); } catch (Exception ex) { throw new SrvrMgrException("Can't start process " + Arrays.toString(runTimeCommand)); } this.locked = false; this.processInput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); this.processOutputStream = process.getInputStream(); this.PID = getProcessId(process); /*TODO: debug*/ processLog(runTimeCommand, "Input Command"); byte[] inputData = new byte[1024]; String result = ""; int numBytes; while ((numBytes = readInputStreamWithTimeout(processOutputStream, inputData, 1000)) > 0 || !result.contains("srvrmgr>")) { if (numBytes > 0) result += new String(inputData, StandardCharsets.UTF_8); else { try { Thread.sleep(500); } catch (InterruptedException ex) { } } if (result.contains(("Fatal error"))) { this.process.destroy(); throw new SrvrMgrException("Could not open connection to Siebel Gateway configuration"); } // System.out.println(result); inputData = new byte[1024]; } /*TODO: debug*/ processLog(result, "Output after process creation"); }
public int compileJava() { try { // create new bin directory boolean createBin = new File(classPath).mkdir(); // create new javac ProcessBuilder // ProcessBuilder pb = // new ProcessBuilder("javac", "-d", classPath, "./" + studentPath + "/*.java"); ProcessBuilder pbDir = new ProcessBuilder("dir"); // Determine current working directory File srcAbsPath = new File(sourcePath); srcAbsPathName = srcAbsPath.getAbsolutePath(); System.out.println("Compiler.java line 69 source path: " + sourcePath); System.out.println("Compiler.java line 69 source absolute path: " + srcAbsPathName); File cwd = pbDir.directory(); // debug code - to confirm correct directory // TestTools.dir(cwd); // NB - ProcessBuilder default is to return a null // pointer for the abstract path to indicate that it // is using System.Properties "user.dir", i.e., the // current system working directory; hence the // critical need to handle a NullPointerException. // Also returns a null pointer if the directory // doesn't exist. // all this is doing is changing the dir, can we approach this in a different way using a // value in our Data Object? -mh File nwd = TestTools.cd(cwd, studentPath); System.out.println("(Compiler.java line 88)new working directory: " + nwd.toString()); String studentPathName = nwd.getAbsolutePath(); File nwdPath = new File(studentPath); System.out.println("(Compiler.java line 91)new working directory path: " + studentPathName); // debug code to test new working directory // TestTools.dir(nwd); FileFilter filter = new FileFilter() {}; String[] javaFileList = nwdPath.list(filter); // set up output file File outputFile = new File(outputFileName); // System.out.println(outputFileName); outputFile.delete(); for (int k = 0; k < javaFileList.length; k++) { try { if (filter.accept(nwdPath, javaFileList[k]) == true) { System.out.println("COMPILER.JAVA (line 111) Compiling: " + javaFileList[k]); String compilePath = "javac" + "-d" + classPath + ".\\" + studentPath + "\\" + javaFileList[k]; System.out.println("Compiler.java 117 compile path: " + compilePath); ProcessBuilder pb = // new ProcessBuilder("javac ", "-d", classPath, ".\\" + studentPath + "\\" + // javaFileList[k]); new ProcessBuilder(compilePath); // System.out.println(pb.environment().toString()); <-- THIS IS VERY INTERESTING // Create environment map and set environmental variables Map<String, String> env = pb.environment(); env.clear(); env.put("PATH", path); env.put("CLASSPATH", classPath); // env.put("SOURCEPATH", sourcePath); // env.remove("OTHERVAR"); pb.redirectErrorStream(true); pb.redirectOutput(Redirect.appendTo(outputFile)); // start javac process Process p = pb.start(); // need other processes to wait for compilation to finish // basically joins the thread to the javac process to force sequential // execution - need to be careful - if any process hangs, whole run hangs success = p .waitFor(); // Returns the exit value of the process. By convention, 0 indicates // normal termination. // http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29 assert pb.redirectInput() == Redirect.PIPE; assert pb.redirectOutput().file() == outputFile; assert p.getInputStream().read() == -1; System.out.println("COMPILER.JAVA (line 138) end of loop, success = " + success); } } catch (Exception e) { System.out.println(" Compiler.java FOR LOOP Compile Exception: " + javaFileList[k]); } } } catch (Exception e) { System.out.println("Compile Exception, PROBABLY DUE TO FILE PATH"); System.out.println("source absolute path: " + srcAbsPathName); } return success; }