今天错新小编将为大家介绍java执行Linux命令的方法,相信很多小伙伴们对于这个问题都是非常陌生的,那么接下来的内容中我们就一起去看看java如何执行Linux命令的内容吧。
try
{
process = Runtime.getRuntime().exec(cmds);
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
try
{
process.waitFor();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
其中参数 cmd 为Linux命令。每次只能执行一条命令。
1.Java Runtime.exec()注意事项:
① 永远要在调用waitFor()方法之前读取数据流
② 永远要先从标准错误流中读取,然后再读取标准输出流
2.最好的执行系统命令的方法就是写个bat文件或是shell脚本。
看完后你知道java如何执行Linux命令了吗?大家在使用的过程中有任何不懂的地方都可以在下方给小编留言询问喔!
新闻热点
疑难解答
图片精选