processbuilder

ProcessBuilder redirecting output

孤者浪人 提交于 2019-11-27 07:12:33
问题 I am trying to redirect output of a process started with the help of ProcessBuilder using following code ProcessBuilder pb = new ProcessBuilder("/myScript >> /myLogFile 2>&1 <& - &"); Map<String, String> env = pb.environment(); env.clear(); env.put("var1", "val1"); env.put("var2", "val2"); pb.redirectErrorStream(true); Process p = pb.start(); But it failed with exception Exception in thread "main" java.io.IOException: Cannot run program "/myScript >> /myLogFile 2>&1 <& - &": java.io

Elevating a ProcessBuilder process via UAC?

不问归期 提交于 2019-11-27 04:25:33
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 = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)

opening a shell and interacting with its I/O in java

回眸只為那壹抹淺笑 提交于 2019-11-27 03:05:31
问题 I am trying to open a shell (xterm) and interact with it (write commands and read the shell's output) Here is a sample of code which won't work: public static void main(String[] args) throws IOException { Process pr = new ProcessBuilder("xterm").start(); PrintWriter pw = new PrintWriter(pr.getOutputStream()); pw.println("ls"); pw.flush(); InputStreamReader in = new InputStreamReader(pr.getInputStream()); System.out.println(in.read()); } When I execute this program an "xterm" window opens and

Setting the environment for ProcessBuilder

夙愿已清 提交于 2019-11-26 23:29:26
问题 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

Process requires redirected input

主宰稳场 提交于 2019-11-26 23:23:19
问题 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! 回答1: ProcessBuilder pb = new ProcessBuilder("prog.exe"); Process p = pb.start(

Start CMD by using ProcessBuilder

陌路散爱 提交于 2019-11-26 22:18:18
问题 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

ProcessBuilder adds extra quotes to command line

本小妞迷上赌 提交于 2019-11-26 22:01:38
问题 I need to build the following command using ProcessBuilder: "C:\Program Files\USBDeview\USBDeview.exe" /enable "My USB Device" I tried with the following code: ArrayList<String> test = new ArrayList<String>(); test.add("\"C:\\Program Files\\USBDeview\\USBDeview.exe\""); test.add("/enable \"My USB Device\""); ProcessBuilder processBuilder = new ProcessBuilder(test); processBuilder.start().waitFor(); However, this passes the following to the system (verified using Sysinternals Process Monitor)

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

早过忘川 提交于 2019-11-26 20:15:31
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. Read from the InputStream . You can append the output to a StringBuilder : BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder builder = new StringBuilder(); String line = null; while ( (line = reader.readLine()) != null) { builder

Java ProcessBuilder: Input/Output Stream

大城市里の小女人 提交于 2019-11-26 18:27:35
问题 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

Process Builder waitFor() issue and Open file limitations

坚强是说给别人听的谎言 提交于 2019-11-26 17:03:16
问题 I have inherited some code: Process p = new ProcessBuilder("/bin/chmod", "777", path).start(); p.waitFor(); Basically, there is for some ancient and highly voodoo based reason for storing key/value pairs on disk as files. I don't really want to go into it. However, I am left with a bunch of IO exceptions: Exception :Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files Message: Cannot run program "/bin/chmod": java.io.IOException: error=24, Too many open files