views:

91

answers:

1

we are trying to re-direct the output of the list to a file using the below cmd(java) in ubuntu, Pls let us know if this works or not ?

Process p = Runtime.getRuntime().exec("ls -l >/home/blah blah/new.txt")

+3  A: 

No it won't. The '>' is part of the shell and as such executing ls on its own won't help.

You can:

  1. capture the output of the ls in Java (capturing the Process input stream, as it's confusingly named) and create a file yourself
  2. use "sh -c 'ls whatever > file'". The -c executes everything following it in a shell, including redirection.
Brian Agnew