How to execute shell script in Java?

经常需要在Java中调用其它的脚本(shell,cmd), 以前都用:

Runtime r = Runtime.getSystemRuntime();
r.exec("whatever you want to run"); 

但是有时侯其运行结果是不可预期的,带来很多麻烦。从java 5.0以后,引入了ProcessBuilder to create operating system processes:

String cmd = "cd ../.. ; ls -l"; // this is the command to execute in the Unix shell
cmd ="cd ~/kaven/Tools/DART ; sh start_dart.sh";
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
pb.redirectErrorStream(true); // use this to capture messages sent to stderr

Process shell = pb.start();
InputStream shellIn = shell.getInputStream(); // this captures the output from the command
int shellExitStatus = shell.waitFor(); // wait for the shell to finish and get the return code

// at this point you can process the output issued by the command
// for instance, this reads the output and writes it to System.out:
int c;
while ((c = shellIn.read()) != -1) {
System.out.write(c);
}
// close the stream
try {
shellIn.close();
}
catch (IOException ignoreMe)
{}
System.out.print(" *** End *** "+shellExitStatus);

System.out.println(pb.command());
System.out.println(pb.directory());
System.out.println(pb.environment());
System.out.println(System.getenv());
System.out.println(System.getProperties()); 

时间: 2024-09-12 14:17:45

How to execute shell script in Java?的相关文章

[转]PHP也可以當成Shell Script

PHP 怎么这么红   最近 PHP(Personal Hypertext Preprocessor) 似乎已经成了这一两年来 Linux/Unix 上最广为大家所使用的网页处理语言﹐它的方便.强大功能与 OpenSource 的特性使得它正逐渐侵蚀到传统 CGI 甚至是 MicroSoft ASP(Active Server Page)的市场﹐几乎各大网站征招人才莫不以会 PHP 作为基本条件.   PHP 确实有这个资格可以这么红﹐原因有下面数点 :   PHP 是 OpenSource 软

Shell Script方式的PHP(转) 这种方式颇有点像PERL的CGI方式。。:)

cgi|perl Shell Script方式的PHP PHP 怎么这么红  最近 PHP(Personal Hypertext Preprocessor) 似乎已经成了这一两年来 Linux/Unix 上最广为大家所使用的网页处理语言﹐它的方便.强大功能与 OpenSource 的特性使得它正逐渐侵蚀到传统 CGI 甚至是 MicroSoft ASP(Active Server Page)的市场﹐几乎各大网站征招人才莫不以会 PHP 作为基本条件.  PHP 确实有这个资格可以这么红﹐原因有下

PHP也可以当成Shell Script使用

PHP 执行档的安装 一般 PHP 作为网页处理语言都是要编译成 Apache 的模块﹐这里当然不么做﹐也因此编译起来很简单﹐只要以 root 的身分进行如下动作 : 解开 php-3.0.xx.tar.gz cd php configure make 编译完之后﹐在 php 目录下有一个可执行档﹐档名为 php﹐将它 copy 到 /usr/local/bin 下即可.注意﹐如果档案太大﹐可以使用 strip 指令将 php 的方式将不必要的信息去除﹐这样档案就会小得多了. 第一个程序 开始撰

shell脚本运行java的jar包,并且系统是没有安装过jre的,不过有jre没安装过的压缩包

问题描述 shell脚本运行java的jar包,并且系统是没有安装过jre的,不过有jre没安装过的压缩包 图中三条线分别是 jre ,java程序的jar包,和shell脚本 当前linux系统没有安装jre和jdk,我要怎么写shell脚本,才能运行jar包啊.是不是运行jar包之前还要在shell脚本中用命令把jre安装了啊.求大神.在线等......最好把脚本写好了.....谢谢,悬赏大大滴 解决方案 **要知道,java程序是运行在句jvm中的,如果你没有jre就是说明连java的运行

Denyhosts shell script

#!/bin/sh #Denyhosts shell script cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2"="$1;}' > /root/blacklist.txt DEFINE="5" for i in `cat /root/blacklist.txt` do IP=`echo $i | awk -F= '{print $1}

linux基础之Shell Script入门介绍_linux shell

linux基础之Shell Script 1 Shell Scipt使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程 1.1 程序书写 复制代码 代码如下: #!/bin/bash# Program:#       This program shows "Hello Wrold" in your screen.# History:# 2013/2/3 on_1y First releasePATH=$PATHexport PATHecho -e "Hello Wo

如何用shell脚本编译java工程

    编译java工程一般直接用IDE或者用Ant.Maven之类的工具,很少有人用纯shell来编译java工程.正好遇到这样一个应该,用这篇博文做一下记录.     案例:本人用eclipse写了一个java project,然后编译打成jar包.     这个可以采用eclipse自带的Export就可以导出jar了.但是为了软件自动化等巴拉巴拉的原因,采用存shell脚本编译.     如图所示,java project的名称为iec104,下面src是源文件目录,bin是所引用的ja

29.13. parallel - build and execute shell command lines from standard input in parallel

并行执行shell命令 $ sudo apt-get install parallel 例 29.5. parallel - build and execute shell command lines from standard input in parallel $ cat *.csv | parallel --pipe grep '13113' 设置块大小 $ cat *.csv | parallel --block 10M --pipe grep '131136688' 原文出处:Netk

7.13. parallel - build and execute shell command lines from standard input in parallel

并行执行shell命令 $ sudo apt-get install parallel 例 7.5. parallel - build and execute shell command lines from standard input in parallel $ cat *.csv | parallel --pipe grep '13113' 设置块大小 $ cat *.csv | parallel --block 10M --pipe grep '131136688' 原文出处:Netki