本文转自:
http://bzhang.iteye.com/blog/407846
Runtime exec()调用shell脚本
有时,我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成。
The class java.lang.Runtime features a static method called getRuntime() , which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class's exec() method. Developers often call this method to launch a browser for displaying a help page in HTML.
exec()有四个重载版本
There are four overloaded versions of the exec() command:
* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);
For each of these methods, a command -- and possibly a set of arguments -- is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.
You can pass three possible input parameters into these methods:
1. A single string that represents both the program to execute and any arguments to that program
2. An array of strings that separate the program from its arguments
3. An array of environment variables
Pass in the environment variables in the form name=value . If you use the version of exec() with a single string for both the program and its arguments, note that the string is parsed using white space as the delimiter via the StringTokenizer class.
注意事项:
1. 当调用的外部命令中包含重定向(<、>),管道( | ) 命令时,exec(String command)的版本不能正确解析重定向、管道操作符。所以需要使用exec(String [] cmdArray)。
如, echo "hello world" > /home/admin/newFile.txt
ls -e | grep java
需要使用如下的调用方式
String []cmdArray = new String[]{ "/bin/sh", "-c", "ls -e | grep java"};
Runtime.getRuntime().exec(cmdArray);
2.
# 永远要在调用waitFor()方法之前读取数据流
# 永远要先从标准错误流中读取,然后再读取标准输出流
The next version of Savant is going to focus heavily on the stand-alone runtime and support for dialects and plugins. Supporting all that is largely handled by using a simple executor framework I wrote around Java 1.4 and lower’s Runtime.exec method. A few things to keep in mind when using this:
1. Always read from the streams prior to calling waitFor. Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can’t store enough from standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever is running until something reads the buffered content from standard out and standard error. I would imagine all platforms suffer from this, but some platforms have larger buffers than others. Needless to say, always read from the streams first.
2. Always read from standard error first. I ran across a bug where some OS platforms will always open standard out, but never close it. What this means is that if you read from standard out first and the process only writes to standard error, you’ll hang forever waiting to read. If you read from standard error first, you’ll always be okay on these platforms because the OS seems to shutdown standard error. I think however, that the best way to handle all cases is to check both standard error and standard out for readiness and only read from them if they have something to offer. The downside I could see here is that error isn’t ready, but eventually will be.
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
在exec()后 立即调用waitFor()会导致进程挂起。
相关文章:
1. When Runtime.exec() won't 关于Runtime的注意事项
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
分享到:
相关推荐
一键部署Linux CentOS,自动安装MySQL、Redis和Java Shell脚本-Linux_install_Sql_Sedis_Java
Shell脚本定时监控tomcat,服务挂掉自动重启
在IT领域,Java是一种广泛使用的编程语言,而Shell脚本则是在Linux环境下广泛使用的一种命令语言和脚本解释器。在某些情况下,Java程序需要调用Shell脚本执行特定的操作,比如访问Linux系统命令或者自动化执行一些...
《Shell脚本学习手册》 Shell,是Linux系统中的一种命令语言和程序设计语言,它由C语言编写,作为用户与操作系统内核交互的桥梁。Shell不仅提供了命令行接口,还允许用户编写脚本程序,实现自动化任务处理。本文将...
我该如何在shell脚本中得到PID。 当我在执行shell脚本时,它会启动一个叫子shell的进程。作为主shell的子进程,子shell将shell脚本中的命令作为批处理运行(因此称为“批处理进程”)。 在某些情况下,你也许想要...
当涉及到启动Java应用程序时,shell脚本可以提供一个方便、可重复且灵活的方法。下面我们将深入探讨如何使用shell脚本来启动Java程序以及相关的知识点。 首先,我们需要了解Java程序的运行方式。Java应用程序通常由...
下面将详细介绍如何使用shell脚本(适用于Unix/Linux系统)和bat脚本(适用于Windows系统)来运行Java程序。 **shell脚本运行Java程序** 在Unix/Linux环境中,我们可以创建一个.sh文件作为shell脚本来执行Java程序...
详细的linux shell脚本启动java代码类。
在java代码中调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell...
在Java程序中调用Unix/Linux主机上的Shell命令,并返回相应执行结果。
Java作为一种跨平台的编程语言,提供了多种方式来调用Shell脚本,实现与操作系统的交互。本文将深入探讨Java如何调用Shell脚本以及相关的知识点。 首先,Java通过Runtime类或ProcessBuilder类可以执行外部程序,...
IDEA中编写并运行shell脚本的实现 IDEA中编写并运行shell脚本的实现是指在Integrated Development Environment(IDE)中编写、配置和运行shell脚本的过程。该过程需要安装bashsupport插件,配置插件,安装git软件,...
在IT领域,Shell脚本和Java是两种非常重要的工具,它们各自在不同的场景下发挥着重要作用。Shell脚本主要用于Linux或Unix系统中的自动化任务执行,而Java是一种跨平台的编程语言,广泛应用于企业级应用、大数据处理...
自启动shell脚本和CPU、内存占用监控脚本能确保Java服务在系统启动时自动运行,并实时监测其性能状态,以便及时发现和处理潜在问题。下面将详细介绍这两个方面的内容。 一、Linux系统Java服务自启动shell脚本 自...
本文将深入讲解如何使用shell脚本来实现数据的导出,并结合Java代码进行调用。 首先,让我们分析给出的`backup.sh`脚本: ```bash su - oracle -c " exp cg23/sa tables=exp_table1,exp_table2 file=/home/oracle/...
linux中java项目需要重启一般使用先找到进程杀掉进程,然后找到项目启动,整个过程不算复杂,但是每次都操作一遍太麻烦,我这里把这个过程整理成shell脚本,大家可以修改里面项目名称,每次直接执行这个命令重启项目...
因此,要在Android上运行shell脚本,我们通常需要通过Java代码来间接实现。 1. **使用Runtime类**:这是最基础的方法,通过`Runtime.getRuntime().exec()`方法执行shell命令。例如: ```java Process process = ...
在Windows和Linux环境中,启动批处理脚本(bat)和shell脚本(sh)是常见的任务,特别是在自动化和脚本执行过程中。`RunScript.java`和`StreamGobbler.java`这两个文件可能就是用来实现这个功能的。 `RunScript....
在Java编程中,有时我们需要与操作系统进行交互,执行一些系统级别的任务,比如自动化运维、文件管理等,这时就可能需要用到调用Shell脚本。本文将详细介绍如何在Java中完美封装shell脚本的调用方法,并提供相关的...