Java代码中执行shell脚本_Process的输入、输出、错误流都需要关闭否则会报IO异常:too many open files.
正确的代码应该这样写:
public static int executeWithExitValue(String shellFile)
{
//脚本文件为NULL或空值
if (null == shellFile || shellFile.equals(""))
{
logger.warn("ShellCommand shellFile is null.");
return -1;
}
if (logger.isDebugEnabled())
{
logger.debug("bash " + shellFile);
}
Process process = null;
int exitValue = 1;
try
{
process = Runtime.getRuntime().exec("bash " + shellFile);
process.waitFor();
exitValue = process.exitValue();
if (logger.isDebugEnabled())
{
logger.debug("the exit value of script is " + exitValue);
}
}
catch (Exception e)
{
logger.error("Execute " + shellFile + " exception:", e);
}
finally
{
if (null != process)
{
try
{
process.getErrorStream().close();
}
catch (IOException e)
{
logger.error("close error stream of process fail.", e);
}
finally
{
try
{
process.getInputStream().close();
}
catch (IOException e)
{
logger.error("close input stream of process fail.", e);
}
finally
{
try
{
process.getOutputStream().close();
}
catch (IOException e)
{
logger.error(
"close output stream of process fail.", e);
}
}
}
}
}
if (logger.isDebugEnabled())
{
logger.debug("end invoke function checkStatus.");
}
return exitValue;
}
分享到:
相关推荐
利用app_process实现免root调用shell_Java_下载.zip
在linux环境中,这三个用户都可以直接在任意目录下执行该shell脚本,可是在java代码中调用shell脚本时,报了如下4个错误: 1、sqlldr: command not found 2、sqlplus: command not found 3、0750: You may need to ...
cat /mnt/log_function.sh #!/bin/bash #log function ####log_correct函数打印正确的输出到日志文件 function log_correct () { DATE=`date “+%Y-%m-%d %H:%M:%S”` ####显示打印日志的时间...log_error打印shell脚本
Table of Contents 1. 前言 ...但是,system函数仅返回命令是否执行成功,而我们可能需要获得shell命令在控制台上输出的结果。例如,执行外部命令ping后,如果执行失败,我们希望得到ping的返回信息。
shell,bat脚本运行java程序, shell,bat脚本运行java程序,
给linux的shell脚本加密(ZT).rar
在java中直接调用ShellUtils的静态方法即可,但涉及到输入输出流的操作,最好是在工作线程中调用,否则会阻塞UI界面。 //执行adb命令, //参数中strCommand为adb命令,第一个布尔值为执行该代码是否需要root,第二个...
详细的linux shell脚本启动java代码类。
Linux_command_line, Linux命令和 shell 脚本简介 LinuxLinux命令和 shell 脚本简介为更完整的资源列表,包括初学者的教程, Linux Linux curated资源列表有关资源的更多信息,请访问脚本教程。 Linux介绍插件什么是...
在Java程序中调用Unix/Linux主机上的Shell命令,并返回相应执行结果。
005_echo_read_cat_管道_tee_标准输入输出和错误_重定向标准输入输出和错误_exec.pdf 006_命令执行顺序.pdf 007_文本过滤_正则表达式.pdf 008_grep家族.pdf 009_awk介绍.pdf 010_sed用法介绍.pdf 011_合并与...
资源名称:Linux Shell脚本学习基础视频资源目录:【】11a00d99b60c4e2eba3440b8aa3a6bdd【】linux_shell脚本编程_01认识shell,如何编写shell脚本和执行【】linux_shell脚本编程_02vivim简单的常用操作【】linux_...
Android中级篇之用JAVA代码执行shell命令
利用shell编程编写的判断IP地址是否合法的脚本
java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本
在开发过程中经常会员到在Linux环境中使用Shell脚本启动和停止java程序的情况,常用的java -jar命令只能运行jar文件,有很多不方便的地方,一旦代码修改就需要重新编辑打包,其实还有很多其他的方式,这个资源中包含...
linux初学者写的小脚本,用来配置JAVA环境变量,纯属练手,不喜勿喷
jxta shell 的java最新源码
Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量).docx
bash_shell笔记_1_脚本基础知识