Run shell commands using java program
807580Aug 14 2009 — edited Aug 14 2009Hi guys,
I am trying to run shell commands like cd /something and ./command with arguments as follows, but getting an exception that ./command not found.Instead of changing directory using "cd" command I am passing directory as an argument in rt,exec().
String []cmd={"./command","arg1", "arg2", "arg3"};
File file= new File("/path");
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd,null,file);
proc.waitFor();
System.out.println(proc.exitValue())
BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
}
catch(Exception e)
{e.printStackTrace();
}
So can anyone please tell me what is wrong with this approach? or is there any better way to do this?
Thanks,
Hardik