- 浏览: 772841 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (208)
- Java (77)
- JavaScript (16)
- UML (1)
- Spring (24)
- Hibernate (11)
- J2EE部署 (18)
- 操作系统 (13)
- struts (11)
- jsp (3)
- J2EE (34)
- 数据库 (22)
- tomcat (4)
- apache (2)
- MyEclipse (13)
- Linux (14)
- Ext (6)
- Weblogic (2)
- 数据库 Oracle 空表导出 (1)
- Oracle (3)
- 编码 乱码 (1)
- 多线程 (5)
- jQuery (2)
- Apache Mina (1)
- ibatis (6)
- abator (1)
- svn (1)
- jvm (1)
- ERwin (2)
- mysql (2)
- ant (1)
- memcache (1)
- dubbo (1)
- PowerDesigner (1)
最新评论
-
di1984HIT:
Shallow heap & Retained heap -
tinguo002:
非常感谢 , 太棒了。
Spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法 -
白天看黑夜:
Apache Mina Server 2.0 中文参考手册(带 ...
Apache Mina – 简单的客户端/服务端应用示例 -
wumingxingzhe:
好文
Shallow heap & Retained heap -
di1984HIT:
学习了!!
工作流(Workflow)和BPM的不同
转自:http://blog.csdn.net/moreorless/archive/2009/05/14/4182883.aspx
有时,我们需要在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:
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
需要使用如下的调用方式
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.
3. 另外一个需要注意的地方是:
如果调用的脚本中存在像sudo这样的需要tty的命令时,使用
这样的调用方式,会为脚本执行创建出一个tty环境,否则,运行过程会提示"sorry, you must have a tty to run xxx"的错误。
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
2. java获取系统MAC地址
http://www.javapipe.com/web/memory_and_the_java_runtime.exec_process.html
3. http://pudding.sharera.com/blog/BlogTopic/31232.htm
有时,我们需要在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.
3. 另外一个需要注意的地方是:
如果调用的脚本中存在像sudo这样的需要tty的命令时,使用
String []cmdArray = new String[]{ "/bin/sh", "-c", "yourscriptname"};
这样的调用方式,会为脚本执行创建出一个tty环境,否则,运行过程会提示"sorry, you must have a tty to run xxx"的错误。
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
2. java获取系统MAC地址
http://www.javapipe.com/web/memory_and_the_java_runtime.exec_process.html
3. http://pudding.sharera.com/blog/BlogTopic/31232.htm
发表评论
-
Eclipse,javaw 通过Proxifile代理ipv6协议问题解决
2015-03-17 18:06 2798myeclipse2010升级到myeclipse2014之后 ... -
初始化EHcache CacheManager时报java.net.UnknownHostException
2014-11-13 11:45 12510工程启动时,报一下异常: [wdfportal] [201 ... -
tomcat7可能带来的问题
2013-06-27 00:31 9841、struts标签校验更加严格,如果struts标签中存在嵌 ... -
iBatis执行insert后返回主键
2013-01-18 23:55 1652iBatis插入数据后,返回主键。级联操作很有用。省去了一次的 ... -
Shallow heap & Retained heap
2012-05-16 17:09 49326所有包含Heap Profling功能的工具(MAT, You ... -
Abator —— IBatis 代码生成工具
2012-04-03 18:31 19351、在eclipse安装abator插件http://ibat ... -
使用Eclipse远程调试Tomcat
2012-03-23 22:56 1512有些时候,调试不得不用外网,比如说做支付宝的支付接口,服务器后 ... -
Java compiler level does not match the version of the installed Java project fac
2012-03-02 11:32 1321问题现象:项目图标报错“Java compiler level ... -
WebService的事务处理
2012-03-01 15:03 1562如果你只是要解决两个系统之间的事务同步问题,可以采用判断服务是 ... -
线程池(java.util.concurrent.ThreadPoolExecutor)的使用
2012-02-29 15:50 2509一、简介 线程池类为 j ... -
myeclipse 颜色设置(保护视力)
2012-02-28 09:29 20911.window -> Preferences -> ... -
Quartz表达式解析
2012-02-08 14:40 809字段 允许值 允许的特 ... -
使用iBatis中报 java.sql.SQLException: 无效的列类型异常
2011-12-15 14:46 2244<!--Content表 插入应的 ... -
非常有用的proxool属性详细解说
2011-12-13 16:19 1612Proxool连接池是sourceforge下的一个开源项目, ... -
在工程中查找自己修改的所有代码
2011-12-09 17:41 1049在工程中查找自己修改的所有代码的方法: 1.工程右键 -&g ... -
如何在Eclipse中安装和使用ibatis插件Abator
2011-12-01 21:26 49761、获得abator: http://ibatis. ... -
newCachedThreadPool线程池
2011-11-20 11:35 43036public static ExecutorService n ... -
Apache Mina – 简单的客户端/服务端应用示例
2011-11-19 23:49 5530转自http://javasight.net/2011/05/ ... -
Class.forName()、Class.forName().newInstance() 、New 三者区别!
2011-11-15 09:18 1263终于明白为什么加载数据库驱动只用Class.forName() ... -
Apache MINA 快速入门指南
2011-11-13 12:04 1661最近用到Socket套接字编程,在服务器监听方面还没有具体思路 ...
相关推荐
`Runtime.exec(String command)`方法用于执行单个命令,而如果需要执行包含多个命令的shell脚本,可以使用`Runtime.exec(String[] cmdArray)`,其中cmdArray是一个包含命令及其参数的字符串数组。 下面是一个简单的...
在Android系统中,由于安全性和权限的限制,直接调用shell脚本并不像在Linux或Unix环境下那样简单。然而,对于非root用户来说,确实有一些方法可以实现对shell脚本的调用,尤其是在开发和调试过程中。下面我们将深入...
在某些情况下,Java程序需要调用Shell脚本执行特定的操作,比如访问Linux系统命令或者自动化执行一些任务。本文将详细解释Java如何调用Shell脚本,包括如何编写Shell脚本和在Java中如何传递参数。 首先,Shell脚本...
Java作为一种跨平台的编程语言,提供了多种方式来调用Shell脚本,实现与操作系统的交互。本文将深入探讨Java如何调用Shell脚本以及相关的知识点。 首先,Java通过Runtime类或ProcessBuilder类可以执行外部程序,...
### Java调用Shell脚本详解 #### 一、前言 在实际开发过程中,经常会遇到需要结合Java程序与Shell脚本进行操作的情况。通过Java调用Shell脚本不仅可以实现复杂的功能,还可以提高程序的灵活性和扩展性。本文将详细...
首先,Java调用shell脚本主要通过`Runtime`类或`ProcessBuilder`类来实现。`Runtime.getRuntime().exec()`方法允许我们执行系统命令,而`ProcessBuilder`提供了更灵活的命令构建和环境变量设置功能。下面是一个基本...
在Java编程中,有时我们需要与操作系统进行交互,执行一些系统级别的任务,比如自动化运维、文件管理等,这时就可能需要用到调用Shell脚本。本文将详细介绍如何在Java中完美封装shell脚本的调用方法,并提供相关的...
在 Java 中,我们可以使用 `Runtime` 类的 `exec()` 方法来调用 Shell 命令。下面是一个简单的示例代码: ```java Process pid = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", "ls -l"}); ``` 在这个...
Java中可以通过`java.lang.Runtime`类中的`exec()`方法来执行外部命令,进而调用Shell脚本或Python脚本。以下是一个基本的例子: ```java public String python(String pythonPath, String[] params) { File file ...
本文将深入讲解如何使用shell脚本来实现数据的导出,并结合Java代码进行调用。 首先,让我们分析给出的`backup.sh`脚本: ```bash su - oracle -c " exp cg23/sa tables=exp_table1,exp_table2 file=/home/oracle/...
本文主要介绍了JAVA如何调用Shell脚本,提供了一种实用的解决方案,即使用JAVA的Runtime.getRuntime().exec()方法调用Shell脚本。下面将详细介绍该方法的实现过程和注意事项。 为什么需要调用Shell脚本 在实际项目...
首先,调用shell脚本的基本过程分为以下几个步骤: 1. **设置脚本执行权限**:在Linux系统中,执行一个文件(如shell脚本)需要有相应的执行权限。在Java中,我们可以通过`Runtime.getRuntime().exec()`方法来执行...
本文将深入探讨如何在Java程序中调用Shell脚本,理解其背后的原理,并提供一系列实用代码示例与应用场景。 ### Java调用Shell的基本原理 Java调用Shell主要依赖于`java.lang.Runtime`类中的`exec()`方法。这个方法...
Java调用shell脚本解决传参和权限问题的方法 Java调用shell脚本解决传参和权限问题的方法是指在Java程序中调用shell脚本来执行某些操作,例如执行系统命令、文件操作等。在调用shell脚本时,可能会遇到传参问题和...
2. **调用Shell脚本**:通过Java的`Runtime.getRuntime().exec()`方法来执行Shell命令或脚本。 下面是一个具体的Java类实现示例,用于调用Shell脚本并记录执行日志: ```java import java.io.*; import java.text....
Java调用Shell脚本执行DataX任务是一种常见的数据迁移或同步操作。DataX是阿里巴巴开源的数据同步工具,它支持多种数据库之间的数据迁移。在实际应用中,我们可能需要根据特定的业务逻辑,如where条件,来定制数据...
总之,Java通过`Runtime.exec()`方法调用Shell命令,实现了与操作系统交互的能力。这种方式在处理系统级任务时非常有用,尤其是在需要执行复杂操作或者利用已有系统工具时。然而,也应当谨慎使用,以防止潜在的安全...
Java可以通过ProcessBuilder或Runtime的方式来调用shell脚本。 ProcessBuilder是一种比较直观的方法,参数的设置也比较方便。例如: ```java ProcessBuilder pb = new ProcessBuilder("./" + RUNNING_SHELL_FILE, ...