processbuilder

Setting the environment for ProcessBuilder

霸气de小男生 提交于 2019-11-28 01:00:31
I have a strange problem setting the Linux environment from Java (1.6); specifically the "PATH" variable. In a nutshell, I have a pipeline for running native processes, which uses java.lang.ProcessBuilder . The user can optionally set the environment variables via a HashMap named environment : ProcessBuilder pb = new ProcessBuilder(args); Map<String, String> env = pb.environment(); if (environment != null) env.putAll(environment); Process process = pb.start(); The env variable gets set properly, if I dump it to the console, with a correct value for the PATH variable. However, running the

Process requires redirected input

穿精又带淫゛_ 提交于 2019-11-28 00:09:09
I have a UNIX native executable that requires the arguments to be fed in like this prog.exe < foo.txt. foo.txt has two lines: bar baz I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java? Of course, ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz"); does not work. Thanks! ProcessBuilder pb = new ProcessBuilder("prog.exe"); Process p = pb.start(); OutputStream pos = p.getOutputStream(); InputStream fis = new FileInputStream("file.txt"); byte[] buffer

ProcessBuilder vs Runtime.exec()

寵の児 提交于 2019-11-27 23:28:11
问题 Which one is better? By better I mean which one has better security, etc. (not ease of use). 回答1: Ease of use is the only real difference between those two. Note that ease of use can lead to security by helping to avoid mis-use. At least on OpenJDK 6 Runtime.exec() is implemented using ProcessBuilder : public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException { return new ProcessBuilder(cmdarray) .environment(envp) .directory(dir) .start(); } 来源: https://stackoverflow

ProcessBuilder environment variable in java

安稳与你 提交于 2019-11-27 18:06:02
问题 I'm trying to add a environment variable for a ProcessBuilder object but then when I call on that new variable in the ProcessBuilder I get an error. this is how I build the Process public class OTU { public static void main(String[] args) throws Exception { ProcessBuilder pb = new ProcessBuilder(); Map<String, String> env = pb.environment(); //set environment variable u env.put("u", "util/"); pb.command("echo $u"); Process p = pb.start(); String output = loadStream(p.getInputStream()); String

Java ProcessBuilder: Input/Output Stream

孤街醉人 提交于 2019-11-27 14:55:18
I want to invoke an external program in java code, then the Google tell me that the Runtime or ProcessBuilder can help me to do this work. I have tried it, and there come out a problem the java program can't exit, that means both the sub process and the father process wait for forever. they are hanging or deadlock. Someone tell me the reason is that the sub process's cache is too small. when it try to give back data to the father process, but the father process don't read it in time, then both of them hang. So they advice me fork an thread to be in charge of read sub process's cache data. I do

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

半腔热情 提交于 2019-11-27 14:47:24
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? Java doesn't do anything in this area. It just uses OS services to create the pipes. All Unix like OSs and Windows behave the same in this

Runtime.getRuntime().exec(cmd) hanging

痞子三分冷 提交于 2019-11-27 13:43:18
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) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } I guess the issue is that

Start CMD by using ProcessBuilder

匆匆过客 提交于 2019-11-27 09:16:34
I am trying to start the CMD application in windows by using the following code, but it doesn't work as expected. Several examples from different websites shows that "cmd" as an argument in the ProcessBuilder construct should work. What do I have to do to make my Java app open the CMD application in windows? public class JavaTest { public static void main(String[] args) { ProcessBuilder pb = new ProcessBuilder("cmd"); try { pb.start(); System.out.println("cmd started"); } catch (IOException e) { System.out.println(e.getMessage()); } } } When I try to use a non-existing application it actually

ProcessBuilder gives a “No such file or directory” on Mac while Runtime().exec() works fine

天大地大妈咪最大 提交于 2019-11-27 09:11:35
I have an application, running on the Playframework, which needs to encode some video files. I used Process pr = Runtime.getRuntime().exec(execCode) for this (and it works perfectly), but as I need both, the output stream and the error stream, I am trying to use ProcessBuilder (as is also recommended). But I cannot get it to work (testing on a MacBook). Is there a fundamental difference between the Runtime method and the ProcessBuilder? This is my code for ProcessBuilder (exactly the same code works when replaced by Runtime.getRuntime().exec() ) String execCode = "/opt/local/bin/ffmpeg -i file

Where does Java's ProcessBuilder look to execute commands?

☆樱花仙子☆ 提交于 2019-11-27 07:58:55
问题 When I execute a command using ProcessBuilder , how does it know where to look for that command? Using this hack/trick I've modified my PATH variable (verified by inspecting processBuilder.environment() ) to be bad (empty, working dir, etc) but ProcessBuilder can still execute sort, echo, bash, etc. just fine. How is it doing this?! Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux. 回答1: The documentation says [...] a command, a list of