public class test{
public static void main(String args[]){
Runtime rt=Runtime.getRuntime();
String str[]={"/bin/sh","-c","sh test.sh"};
Process pcs=rt.exec(str);
BufferedReader br = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
String line=new String();
while((line = br.readLine()) != null)
{
System.out.println(line);
}
try{
pcs.waitFor();
}
catch(InterruptedException e){
System.err.println("processes was interrupted");
}
br.close();
int ret=pcs.exitValue();
System.out.println(ret);
}
}
java调用shell的问题
本文展示了一个使用Java程序调用外部Shell脚本的例子。通过Runtime类获取运行时实例,并构造Shell命令数组,最终执行并读取输出结果。此示例涵盖了进程创建、输入输出流处理及进程等待等关键步骤。

被折叠的 条评论
为什么被折叠?



