Monday, 19 August 2013

Java - Command Execution in Runtime

Java - Command Execution in Runtime

I tried out a simple program to execute Linux command at run time. But the
following program getting compiled and runs without any error, but the
text file is not getting created as intended.Is there anything wrong in
this program?
import java.io.*;
class ExecuteJava
{
public static void main(String args[])
{
String historycmd = "cat ~/.bash_history >>
Documents/history.txt";
try
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(historycmd);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

No comments:

Post a Comment