How to make this work on windows , the file filename.txt is not being created.
Process p = Runtime.getRuntime().exec("cmd echo name > filename.txt");
Clearly the expected output is a "filename.txt" should be created (C:\Documents and Settings\username\filename.txt ) with the content "name".
Was able to manage with the following code , even though the file was"filename.txt" is not being created with processBuilder
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c cleartool lsview");
// Directly to file
//Process p = Runtime.getRuntime().exec(
// new String[] { "cmd", "/c", "cleartool lsview > filename.txt" },null, new File("C:/Documents and Settings/username/"));
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
OR , using ProceessBuilder ,
Process process = new ProcessBuilder( "cmd", "/c", "cleartool lsview" ).start();
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.printf("Output of running %s is:", Arrays.toString(args));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
2 Answers 2
You should actually be using ProcessBuilder instead of Runtime.exec (see the docs).
ProcessBuilder pb = new ProcessBuilder("your_command", "arg1", "arg2");
pb.directory(new File("C:/Documents and Settings/username/"));
OutputStream out = new FileOutputStream("filename.txt");
InputStream in = pb.start().getInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
out.close();
(I'd adapt it to cmd and echo if I had a windows-machine in reach... Feel free to edit this post!)
9 Comments
ProcessBuilder was introduced in JDK 5. Earlier version of the JDK will still require Runtime.exec(...).ProcessBuilder or exec().your_command probably corresponds to cmd and arg1 to echo name. However, I don't have a windows-machine available to test this. (That's why I said that everybody are free to edit the post.)new ProcessBuilder("cleartool", "lsview"); in the above solution.It should work with
Process p = Runtime.getRuntime().exec(
new String[] { "cmd", "/c", "echo name > filename.txt" });
I don't have Windows running at the moment so unfortunately I can't test it.
The reason behind this is that in your version, the command gets splitted at every space character. So what the runtime does is create a process cmd and feed it the arguments echo, name, > and filename.txt which makes no sense. The command echo name > filename.txt is a single argument to the cmd process and so you have to provide an array with the different arguments manually.
If you want to make sure that the file is created in a particular folder you have to provide a working directory to exec() which only works in the three argument version:
Process p = Runtime.getRuntime().exec(
new String[] { "cmd", "/c", "echo name > filename.txt" },
null, new File("C:/Documents and Settings/username/"));
5 Comments
Runtime.exec could handle the redirection in echo name > filename.txtcmd which handles redirection. Not exec().
java.io.File?cmd /k echo name > filename.txt?