如何在java程序中调用linux命令或者shell脚本
展开全部
@Test
public void test6() {
BufferedReader br = null;
Runtime rt = Runtime.getRuntime();
String cmdstr = "cmd.exe /c dir";
try {
Process process = rt.exec(cmdstr);
//输入执行结果
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
if (process != null) {
process.destroy();
process = null;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询