public static boolean execute(String shellFile)
{
//脚本文件为NULL或空值
if (null == shellFile || shellFile.equals(""))
{
logger.warn("ShellCommand shellFile is null.");
return false;
}
if (logger.isDebugEnabled())
{
logger.debug("bash " + shellFile);
}
try
{
Process process = Runtime.getRuntime().exec("bash " + shellFile);
int iretCode = process.waitFor();
try
{
if (iretCode != 0)
{
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
StringBuilder errorDesc = new StringBuilder();
for (String str = br.readLine(); str != null; str = br
.readLine())
{
errorDesc.append(str);
}
//zkf35483 新增关闭流操作
br.close();
logger.error("execute shell " + shellFile + " failed: "
+ errorDesc);
}
else
{
return true;
}
}
catch (IOException e)
{
logger.error("IOException:", e);
}
finally
{
process.getErrorStream().close();
process.getInputStream().close();
process.getOutputStream().close();
}
}
catch (Exception e)
{
logger.error("Execute " + shellFile + " exception:", e);
}
return false;
}
ShellCommand_Java代码中执行shell脚本_Process的输入、输出、错误流都需要关闭(2)
最新推荐文章于 2022-05-21 11:39:32 发布
本文介绍了一种在Java中执行Shell脚本的方法,并详细展示了如何处理执行过程中的异常及输出错误信息。
845

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



