2

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);
}
Andrew Thompson
169k42 gold badges224 silver badges441 bronze badges
asked Sep 23, 2010 at 11:04
8
  • 1
    how it doesn't work now? Commented Sep 23, 2010 at 11:06
  • In what way does it not work? Commented Sep 23, 2010 at 11:06
  • Clearly? are you running it straight from `C:\Documents and Settings\username` folder? Commented Sep 23, 2010 at 11:12
  • 4
    why are you actually trying to use cmd for that? Can't you do it with java.io.File? Commented Sep 23, 2010 at 11:27
  • The command you're executing opens a command window on my machine. Do you mean cmd /k echo name > filename.txt? Commented Sep 23, 2010 at 11:50

2 Answers 2

6

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!)

Sign up to request clarification or add additional context in comments.

9 Comments

Bear in mind that ProcessBuilder was introduced in JDK 5. Earlier version of the JDK will still require Runtime.exec(...).
The root of the problem (passing a single string which is splitted at the wrong places) stays the same whether you use ProcessBuilder or exec().
My actual need is "cmd cleartool lsview > views.txt" , the execpect output from the command cleartool lsview should goto views.txt file. This is working fine with normal cmd prompt. But within the java program i did like to achieve this
@musiKk; You're right. I could clarify that 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.)
@srinannapa, the try to do new ProcessBuilder("cleartool", "lsview"); in the above solution.
|
4

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/"));
answered Sep 23, 2010 at 11:08

5 Comments

Platform dependent solution though.
it's a platform-dependent question anyway :)
Yes Bozho. I don't really get that comment. If you exec something it is (almost?) always platform dependent.
Will this work? I didn't think Runtime.exec could handle the redirection in echo name > filename.txt
@tim: That's why it's an argument to cmd which handles redirection. Not exec().

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.