processbuilder

Process.waitFor() a thread

被刻印的时光 ゝ 提交于 2019-12-10 10:54:28
问题 While running an external script, I want to read the ErrorStream and OutputStream of this script both simultaneously and separately and then process them further. Therefore, I start a Thread for one of the streams. Unfortunately, the Process doesn't seem to waitFor the Thread to be terminated, but return after the non-threaded stream has no further input. In a nutshell, here is what I am doing: ProcessBuilder pb = new ProcessBuilder(script); final Process p = pb.start(); new Thread(new

Error trying to mock constructor for ProcessBuilder using PowerMockito

喜夏-厌秋 提交于 2019-12-10 10:46:01
问题 I am trying to mock the constructor for ProcessBuilder. The problem is that when the constructor is called it return null. Class code: public static void enable() throws IOException, InterruptedException { logger.info("Enable NTP server..."); String ntpAddress = AppConfig.getInstance().getString(AppConfig.NTP_SERVER, ""); AppConfig.getInstance().getBoolean(AppConfig.NTP_ENABLED, true); String enableNtp = "chkconfig ntpd on " + SEPARATOR + " service ntpd stop " + SEPARATOR + " ntpdate " +

Running bat file with java processbuilder

微笑、不失礼 提交于 2019-12-10 09:49:30
问题 I am trying to execute .bat file using java process builder but it does not starts the process. Please tell me what i am doing wrong here. This code works fine with linux envoirnment when I replace file.bat with ./file.sh final ArrayList<String> command = new ArrayList<String>(); command.add(WORKING_DIR+File.separator+"file.bat"); final ProcessBuilder builder = new ProcessBuilder(command); try { builder.redirectErrorStream(true); builder.start(); } catch (IOException e) { logger.error("Could

java.io.IOException: error=11

夙愿已清 提交于 2019-12-10 03:52:03
问题 I am experiencing a weird problem with the Java ProcessBuilder . The code is shown below (in a slightly simplified form) public class Whatever implements Runnable { public void run(){ //someIdentifier is a randomly generated string String in = someIdentifier + "input.txt"; String out = someIdentifier + "output.txt"; ProcessBuilder builder = new ProcessBuilder("./whateveer.sh", in, out); try { Process process = builder.start(); process.waitFor(); } catch (IOException e) { log.error("Could not

How do I Pipe process output to a file on Windows and JDK 6u45

别说谁变了你拦得住时间么 提交于 2019-12-08 18:38:03
问题 I have the following windows batch file (run.bat): @echo off echo hello batch file to sysout And the following java code, which runs the batch files and redirects output to a file: public static void main(String[] args) throws IOException { System.out.println("Current java version is: " + System.getProperty("java.version")); ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "run.bat" ,">>", "stdout.txt","2>>", "stderr.txt" ); System.out.println("Command is: " + pb.command()); Process

How to open any command line program through a swing GUI and pass commands to it?

…衆ロ難τιáo~ 提交于 2019-12-08 09:27:18
问题 I basically want to convert a command line program into a gui program using Swing. As soon as the user presses an appropriate button, a corresponding command should be passed by the GUI to the command line program. If we can do this without showing the command line program then it would be a complete replacement of that program. I have been trying to search this on the internet for the past two days now and I only found the Runtime.getRuntime().exec(cmd) command useful to open the command

Java Processbuilder Stream to Python-Script

风流意气都作罢 提交于 2019-12-08 08:47:14
问题 I have a minor Problem with a small Project I'm trying to do. I'm trying to use a Java-Program to call a Python-Script. Java: ProcessBuilder pb = new ProcessBuilder("python3", "tmp.py"); process = pb.start(); OutputStream stdin = process.getOutputStream(); InputStream stderr = process.getErrorStream(); InputStream stdout = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin

Collect environment variable in java after running script through ProcessBuilder

巧了我就是萌 提交于 2019-12-08 08:30:46
问题 Why does the the following code print false ? I am trying to an environment variable in the test.sh script and collect it in java. Please suggest an alternative approach, if possible. public static void main(String[] args){ ProcessBuilder processBuilder = new ProcessBuilder("test.sh"); Process process; int exitCode; try { process = processBuilder.start(); exitCode = process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated

Problem ProcessBuilder running script sh

左心房为你撑大大i 提交于 2019-12-08 04:18:49
问题 trying to execute an script, using this piece of code: String command = "./myScript.sh"; pb = new ProcessBuilder(command, param1, param2); pb.directory(directory); pb.start(); I am not getting any kind of error, but neither the supposed results. Anyway, I tryed to run the same command, direclty in the terminal, and everything working correctly. Am I missing something?? Thanks in advance 回答1: When you start a process ( pb.start() ) you get back a Process instance. If your script reads input or

Collect environment variable in java after running script through ProcessBuilder

穿精又带淫゛_ 提交于 2019-12-08 03:51:25
Why does the the following code print false ? I am trying to an environment variable in the test.sh script and collect it in java. Please suggest an alternative approach, if possible. public static void main(String[] args){ ProcessBuilder processBuilder = new ProcessBuilder("test.sh"); Process process; int exitCode; try { process = processBuilder.start(); exitCode = process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, String>envVars = processBuilder.environment(); System.out