how use Runtime.getRuntime().exec()?
I try:
try {
String[] callAndArgs = {"path\\program.exe",开发者_运维知识库"arg1","arg2","arg3"}
System.out.println("before");
Process p = Runtime.getRuntime().exec(callAndArgs)
System.out.println("in");
p.waitFor();
System.out.println("after");
} catch (IOException e) {
System.out.println("catch io: " + e.getMessage());
} catch (InterruptedException ie) {
System.out.println("catch ie: " + ie.getMessage());
}
And program doesn't work. Output:
before
in
after
hm? help ;p
See this (classic) article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Maybe this could help too (http://ubuntuforums.org/showthread.php?t=681779),
import java.lang.* ;
public class MyJavaClass
{
public void runCmd(String[] args)
{
String cmd = "/home_dir/./my_shell_script.sh" ;
Runtime run = Runtime.getRuntime() ;
Process pr = run.exec(cmd) ;
pr.waitFor() ;
BufferedReader buf = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;
while ( ( String line ; line = buf.readLine() ) != null )
{
System.out.println(line) ;
}
}
}
精彩评论