利用Runtime.execute方法,我们可以在Java程序中运行Linux的Shell脚本,或者执行其他程序。参考了互联网上的这篇文章,我重新整理了代码。

现在通过CommandHelper.execute方法可以执行命令,该类实现代码如下:

package javaapplication3;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

*

* @author chenshu

*/

public class CommandHelper {

//default time out, in millseconds

public static int DEFAULT_TIMEOUT;

public static final int DEFAULT_INTERVAL = 1000;

public static long START;

public static CommandResult exec(String command) throws IOException, InterruptedException {

Process process = Runtime.getRuntime().exec(command);

CommandResult commandResult = wait(process);

if (process != null) {

process.destroy();

}

return commandResult;

}

private static boolean isOverTime() {

return System.currentTimeMillis() - START >= DEFAULT_TIMEOUT;

}

private static CommandResult wait(Process process) throws InterruptedException, IOException {

BufferedReader errorStreamReader = null;

BufferedReader inputStreamReader = null;

try {

errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

//timeout control

START = System.currentTimeMillis();

boolean isFinished = false;

for (;;) {

if (isOverTime()) {

CommandResult result = new CommandResult();

result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);

result.setOutput("Command process timeout");

return result;

}

if (isFinished) {

CommandResult result = new CommandResult();

result.setExitValue(process.waitFor());

//parse error info

if (errorStreamReader.ready()) {

StringBuilder buffer = new StringBuilder();

String line;

while ((line = errorStreamReader.readLine()) != null) {

buffer.append(line);

}

result.setError(buffer.toString());

}

//parse info

if (inputStreamReader.ready()) {

StringBuilder buffer = new StringBuilder();

String line;

while ((line = inputStreamReader.readLine()) != null) {

buffer.append(line);

}

result.setOutput(buffer.toString());

}

return result;

}

try {

isFinished = true;

process.exitValue();

} catch (IllegalThreadStateException e) {

// process hasn't finished yet

isFinished = false;

Thread.sleep(DEFAULT_INTERVAL);

}

}

} finally {

if (errorStreamReader != null) {

try {

errorStreamReader.close();

} catch (IOException e) {

}

}

if (inputStreamReader != null) {

try {

inputStreamReader.close();

} catch (IOException e) {

}

}

}

}

}

CommandHelper类使用了CommandResult对象输出结果错误信息。

linux脚本运行java程序设计_Java运行Shell脚本相关推荐

  1. linux中启动 java -jar 后台运行程序

    ps -ef | grep java  查询java 端口占用 linux中启动 java -jar 后台运行程序 直接用java -jar xxx.jar,当退出或关闭shell时,程序就会停止掉. ...

  2. linux脚本调用db2存储过程,LINUX定时执行含有DB2存储过程的SHELL脚本

    <LINUX定时执行含有DB2存储过程的SHELL脚本>由会员分享,可在线阅读,更多相关<LINUX定时执行含有DB2存储过程的SHELL脚本(6页珍藏版)>请在人人文库网上搜 ...

  3. linux停止jar程序,Linux 启动停止SpringBoot jar 程序部署Shell 脚本的方法

    废话不多说了,先给大家上代码,具体代码如下所示: #!/bin/bash cd `dirname $0` CUR_SHELL_DIR=`pwd` CUR_SHELL_NAME=`basename ${ ...

  4. linux shc shell脚本_使用shc工具加密shell脚本详解

    Shc可以用来对shell脚本进行加密,可以将shell脚本转换为一个可执行的二进制文件.经过shc对shell脚本进行加密后,会同时生成两种个新的文件,一个是加密后的可执行的二进制文件(文件名以.x ...

  5. linux shell rsync,linux – 如何在我的下面的shell脚本中使用rsync而不是scp来复制文件?...

    我正在使用scp并行使用GNU并行复制文件与我的下面的 shell脚本,它工作正常. 我不知道如何在下面的shell脚本中使用rsync代替scp.我试图看看rsync与传输速度方面的scp相比是否会 ...

  6. linux启动脚本springboot,Linux 启动停止SpringBoot jar 程序部署Shell 脚本的方法

    废话不多说了,先给大家上代码,具体代码如下所示: #!/bin/bash cd `dirname $0` cur_shell_dir=`pwd` cur_shell_name=`basename ${ ...

  7. Java程序定时执行shell脚本

    第一次写博客,写的不好还请见谅. 之前在Linux环境中想定期执行某个脚本,第一反应就是将这个task加入到crontab里(crontab的知识点这里就不具体介绍了),当然,这种做法一般情况下是可行 ...

  8. linux脚本查看系统内存,二个linux下查看内存使用情况的shell脚本()

    摘要 腾兴网为您分享:二个linux下查看内存使用情况的shell脚本(),政务易,悦作业,优化大师,王者荣耀等软件知识,以及单向历app,优路教育app,kimoji,开关电源设计软件,皮皮高清影视 ...

  9. SHELL/SSH基础知识(入门篇)-包含 shell 脚本语言的基本用法、 shell 脚本语言的基本用法、流程控制、函数 function、其它脚本相关工具、数组 array(欢迎留言交流)

    目录 1 shell 脚本语言的基本用法 1.1 shell 脚本注释规范 1.1.1 shell 脚本注释规范 1.1.2 执行(5种) 1.1.3 在远程主机运行本地脚本 1.1.4 检查shel ...

最新文章

  1. 类名.class的含义。
  2. Flutter中关键字Const和Final之间的区别
  3. Shared pool内存块组成结构及4031错误原因分析
  4. Webservice接口、Webservice例子
  5. VC/MFC如何设置对话框背景颜色
  6. maven进阶:一个多模块项目
  7. Oracle学习(十四)分表分区
  8. linux sd卡 u盘区别,linux下实现U盘和sd卡的自动挂载
  9. ar开发 ue4_UE4 和 ARKit 入门
  10. 有了这些接口测试用例+工具,测试效率想不提升都难
  11. 所有手机品牌型号大全_【干货】史上最全SMT贴片机品牌、型号大全,赶紧看看你会几种???...
  12. PyGame:Python 游戏编程入门-1
  13. 火星时代室内效果图风暴10CD B
  14. UWB人员定位解决方案
  15. Bootstrap4模态框垂直居中
  16. texlive写论文源代码_使用Latex写论文
  17. np.meshgrid plt.contourf
  18. 用python使图形动起来?
  19. Esri总裁用96页PPT告诉你GIS的未来将如何发展,而我们又该为此如何准备
  20. 虚拟机(虚拟机安装教程win10)

热门文章

  1. pandas中apply与map的异同
  2. c语言程序设计修改题,2006年秋季C语言程序设计》期末考试题A卷答案答案--修改后.doc...
  3. mysql5.7学习nosql_如何学习NoSQL?
  4. GAN生成对抗网络-text to image原理与基本实现-文字转图像-11
  5. linux server文件,linux两台server远程copy文件
  6. linux安装多版本php_Linux系统下为Nginx安装多版本PHP
  7. vue中使用v-on绑定事件中,获取$event.currentTarget,日志打印为null
  8. 阿里云服务器重启出现An error occurred 如何处理
  9. ●BZOJ 4556 [Tjoi2016Heoi2016]字符串
  10. “GIS讲堂”第二课内容的公布