I am trying to execute a program from Java code. Here is my code:
public static void main(String argv[]) { try { String line; Process p = Runtime.getRuntime().exec( "/bin/bash -c ls > OutputFileNames.txt"); BufferedReader input = new BufferedReader( new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { System.out.println(line); } input.close(); } catch (Exception err) { err.printStackTrace(); } }
My OS is Mac OS X 10.6.
If I remove "> OutputFileNames.txt" from the getRuntime().exec() method, all the file names will be printed to the console just fine. But I need this to be printed in the file.
Also, if I changed the command to:
Process p = Runtime.getRuntime().exec( "cmd \c dir > OutputFileNames.txt");
and run it on Windows, it launches and prints the results in a file also fine.
I read other messages to run another Java application, but none of them dealt with my problem.
I would really appreciate any help I can get.
Thanks,
java terminal macos
Saurabh lalwani
source share