Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Run shell commands using java program

807580Aug 14 2009 — edited Aug 14 2009
Hi 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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 11 2009
Added on Aug 14 2009
27 comments
697 views