processbuilder

Why does process hang if the parent does not consume stdout/stderr in Java?

南笙酒味 提交于 2019-11-26 16:54:32
问题 I know that if you use ProcessBuilder.start in Java to start an external process you have to consume its stdout/stderr (e.g. see here). Otherwise the external process hangs on start. My question is why it works this way. My guess is that JVM redirects stdout/stderr of the executed process to pipes and if the pipes have no room the writes to the pipes block. Does it make sense? Now I wonder why Java does it way. What is the rationale behind this design? 回答1: Java doesn't do anything in this

How to set working directory with ProcessBuilder

巧了我就是萌 提交于 2019-11-26 16:37:24
I am trying start a process in my home directory in ubuntu. I am getting an array out of bounds exception. Here is the code: Process p = null; ProcessBuilder pb = new ProcessBuilder(); pb.directory(new File("/home")); p = pb.start(); Here is the exception: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at java.lang.ProcessBuilder.start(ProcessBuilder.java:459) at tester.Main.main(Main.java:31) Java Result: 1 dmeister You are trying to execute /home and it is not an executable file. The constructor argument of the process builder is the command to execute. You want to

Runtime.getRuntime().exec(cmd) hanging

巧了我就是萌 提交于 2019-11-26 16:30:49
问题 I am executing a command which returns me the Revision number of a file; 'fileName'. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code. String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; Process p = Runtime.getRuntime().exec(cmd) ; BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = in.readLine()) != null) {

Elevating a ProcessBuilder process via UAC?

落花浮王杯 提交于 2019-11-26 11:07:22
问题 I\'m trying to run an external executable, but apparently it needs elevation. The code is this, modified from an example of using ProcessBuilder (hence the array with one argument) : public static void main(String[] args) throws IOException { File demo = new File(\"C:\\\\xyzwsdemo\"); if(!demo.exists()) demo.mkdirs(); String[] command = {\"C:\\\\fakepath\\\\bsdiff4.3-win32\\\\bspatch.exe\"}; ProcessBuilder pb = new ProcessBuilder( command ); Process process = pb.start(); InputStream is =

How to redirect Process Builder's output to a string?

岁酱吖の 提交于 2019-11-26 07:28:42
问题 I am using the following code to start a process builder.I want to know how can I redirect its output to a String. ProcessBuilder pb = new ProcessBuilder(System.getProperty(\"user.dir\")+\"/src/generate_list.sh\", filename); Process p = pb.start(); I tried using ByteArrayOutputStream but it didn\'t seem to work. 回答1: Read from the InputStream . You can append the output to a StringBuilder : BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

How to set working directory with ProcessBuilder

ε祈祈猫儿з 提交于 2019-11-26 04:51:42
问题 I am trying start a process in my home directory in ubuntu. I am getting an array out of bounds exception. Here is the code: Process p = null; ProcessBuilder pb = new ProcessBuilder(); pb.directory(new File(\"/home\")); p = pb.start(); Here is the exception: Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 0 at java.lang.ProcessBuilder.start(ProcessBuilder.java:459) at tester.Main.main(Main.java:31) Java Result: 1 回答1: You are trying to execute /home and it is not an

Java ProcessBuilder: Resultant Process Hangs

那年仲夏 提交于 2019-11-26 03:59:11
问题 I\'ve been trying to use Java\'s ProcessBuilder to launch an application in Linux that should run \"long-term\". The way this program runs is to launch a command (in this case, I am launching a media playback application), allow it to run, and check to ensure that it hasn\'t crashed. For instance, check to see if the PID is still active, and then relaunch the process, if it has died. The problem I\'m getting right now is that the PID remains alive in the system, but the GUI for the

Pass String as params from one Java App to another

浪尽此生 提交于 2019-11-26 03:45:21
问题 I\'m trying to pass String as parameter from one Java Aplications to second as StartUp parameter for example I have Aplications that must call start another Java Aplication (just contains only JOptionPane, JDialog or simple JFrame) before System.exit(0); , there I trying to send some descriptions from closing application to another, these code is simulations what I tried that and in this form, code works correctly and displayed String into the JTextArea ... import java.io.IOException; import

Executing another application from Java

邮差的信 提交于 2019-11-26 02:00:50
I need to execute a batch file which executes another Java application. I don't care whether it executes successfully or not and I don't have to capture any errors. Is it possible to do this with ProcessBuilder ? What are the consequences if I do not capture errors? However, my requirement is just to execute another Java application. The Runtime.getRuntime().exec() approach is quite troublesome, as you'll find out shortly. Take a look at the Apache Commons Exec project. It abstracts you way of a lot of the common problems associated with using the Runtime.getRuntime().exec() and ProcessBuilder

ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread

两盒软妹~` 提交于 2019-11-26 01:35:48
问题 I\'m building a process in Java using ProcessBuilder as follows: ProcessBuilder pb = new ProcessBuilder() .command(\"somecommand\", \"arg1\", \"arg2\") .redirectErrorStream(true); Process p = pb.start(); InputStream stdOut = p.getInputStream(); Now my problem is the following: I would like to capture whatever is going through stdout and/or stderr of that process and redirect it to System.out asynchronously. I want the process and its output redirection to run in the background. So far, the