runtime.exec

How to set an environment variable in Java using exec? [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-27 23:09:53
Possible Duplicate: How do I set environment variables from Java? I'm trying to set an environment variable, and read it back to verify it was actually set. I've got the following : import java.io.IOException; public class EnvironmentVariable { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("cmd.exe set FOO=false"); String s = System.getenv("FOO"); System.out.println(s); } } However, it appears that FOO is always null, meaning its probably not set correctly. Do I have the exec command correct? The javadocs state it can take a string argument as the

Run a sub process, provide input and output to it correctly in Java

江枫思渺然 提交于 2019-11-27 23:08:20
I use Runtime exec() method to create a subprocess in Java. However, since the subprocess is an interactive program, I need to provide input to it as and when required by it. Also I need to show the output of the subprocess. How can I do this in the simplest possible way? I was using a StreamGobbler to show the program output using process.getInputStream(). I, however, do not know how to identify when the program is waiting for input and when to provide it input using proc.getOutputStream. How can I do this? vitaut You need to copy the input and output between the subprocess' streams and

Java Runtime exec() fails to escape characters properly

别说谁变了你拦得住时间么 提交于 2019-11-27 14:24:14
This might already been answered before but that was regarding unicode and I don't think this is unicode (it's in ASCII so...). When I execute this in my terminal there is no problem what so ever vboxmanage setextradata "Test Machine" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222 However when I use the following in Java Runtime.getRuntime().exec("vboxmanage setextradata \"Test Machine\" \"VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort\" 2222"); It returns an error: unregistered vm '"TestMachine"' The same goes for parameters with spaces in them like Test\ Machine, then

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

Running Bash commands in Java

我是研究僧i 提交于 2019-11-27 12:52:49
I have the following class. It allows me to execute commands through java. public class ExecuteShellCommand { public String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); } } When I run commands, the result of the previous command isn't saved. For

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

Running shell script from java code and pass arguments

主宰稳场 提交于 2019-11-27 08:38:02
问题 I am executing a shell script from Java program. I have implemented it using Runtime class. Below is the code I implemented final StringBuilder sb = new StringBuilder("test.sh"); sb.append("/path to/my/text file"); final Process p = Runtime.getRuntime().exec(sb.toString()); Here sb is string buffer object where I append my parameters and use it in exec method. But the problem is the parameter I pass "/path to/my/text file" is considered as 4 parameters /path to /my/text file But if run in

$PATH variable isn't inherited through getRuntime().exec

99封情书 提交于 2019-11-27 07:58:25
问题 I'm trying to start a script by the following command in Java: proc = Runtime.getRuntime().exec(cmd, null, fwrkDir); The command, typed in a console, works flawlessly. But here it doesn't seem to find the script, even though it's path is added to the $PATH variable. Doesn't Java automatically inherit all such variables, if null is passed as Environment? 回答1: proc = Runtime.getRuntime().exec(cmd, null, fwrkDir); should be proc = Runtime.getRuntime().exec(cmd, "PATH=$PATH:/android-sdk-linux_x86

set windows PATH environment variable at runtime in Java

倾然丶 夕夏残阳落幕 提交于 2019-11-27 06:59:38
问题 I have a java program that fires off an executable using the Runtime.exec() method. I'm using the variant that takes in a set of command line params as one argument, and some environment variables as another argument. The environment variable I'm tryign to set is path, so i'm passing in "PATH=C:\some\path". This does not work. Is there some trick to this or any alternatives. I am stuck to Java 1.4 unfortunately. 回答1: use http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv