- 浏览: 1527662 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (225)
- JAVA (27)
- Spring (49)
- Linux (51)
- JavaScript (8)
- Scrum (23)
- IDE (2)
- JSON (2)
- Solr (0)
- Webharvest (0)
- Hibernate (8)
- 杂谈 (3)
- Windows 7 (4)
- 持续集成 (23)
- tomcat (3)
- Android (1)
- SpringSecurity (11)
- Maven (9)
- jotm (3)
- C3P0 (1)
- Active Directory (2)
- cas (1)
- JQuery (2)
- ajax (1)
- plsql (2)
- nginx (4)
- apache (1)
- thrift (7)
- python (3)
- oracle (4)
- php (2)
- redis (1)
- fedora (1)
- windows7 (0)
- SVN (1)
- NFS (1)
- SAMBA (1)
- Atomikos (1)
- apache-poi (1)
- mysql (2)
- vncserver (1)
- mac (2)
- firefox (1)
- JIRA (1)
- p6spy (1)
- git (1)
- github (1)
- gitlab (1)
- gogs (1)
- Druid (1)
- MyBatis (1)
- docker (8)
- zabbix (1)
最新评论
-
lialatd:
您好,我用您的方法通过java api往jira系统中添加is ...
JIRA REST API ---- JAVA -
sprcen945:
可以了,是因为没加intercept-url 的拦截, 尼玛, ...
SpringSecurity3.X--Cas client 配置 -
sprcen945:
请问为什么我配了security.xml后切入点不起作用(之前 ...
SpringSecurity3.X--Cas client 配置 -
linxingyul:
根据楼主的代码 继承了WebMvcConfigurationS ...
SpringMVC4零配置--Web上下文配置【MvcConfig】 -
java_老头:
MvcConfig.java的FilterType.ANNOT ...
SpringMVC4零配置--Web上下文配置【MvcConfig】
原文之地:http://blog.csdn.net/Nicolas_Yang/archive/2008/12/12/3504480.aspx
【JAVA 中执行Shell】
jShell.java
/*
* jShell.java
* class jShell is used for executing shell command
* USAGE:
* jShell obj=new jShell(shellCommand);
* obj.startErr();
* obj.startOut();
* obj.startIn();
* You can Interupt I/O thread when nessasary:
* obj.interruptErr();
* obj.interruptOut();
* obj.interruptIn();
*
* BY Ahui Wang Nankai U. 2007-05-12
*/
import java.io.*;
public class jShell {
Thread tIn; //handle input of child process
Thread tOut;//handle output of child process
Thread tErr;//handle error output of child process
public jShell(String shellCommand){
Process child=null; //child process
try{
child=Runtime.getRuntime().exec(shellCommand);
}
catch(IOException e){
e.printStackTrace();
}
if(child==null){
return;
}
final InputStream inputStream=child.getInputStream();
final BufferedReader brOut=
new BufferedReader(new InputStreamReader(inputStream));
tOut=new Thread(){ //initialize thread tOut
String line;
int lineNumber=0;
public void run(){
try{
while((line=brOut.readLine())!=null){
System.out.println(lineNumber+". "+line);
lineNumber++;
}
}
catch(IOException e){
e.printStackTrace();
}
}
};
final InputStream errorStream=child.getErrorStream();
final BufferedReader brErr=
new BufferedReader(new InputStreamReader(errorStream));
tErr=new Thread(){ //initialize thread tErr
String line;
int lineNumber=0;
public void run(){
try{
while((line=brErr.readLine())!=null){
System.out.println(lineNumber+". "+line);
lineNumber++;
}
}
catch(IOException e){
e.printStackTrace();
}
}
};
// read buffer of parent process' input stream
final BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
final OutputStream outputStream = child.getOutputStream();
tIn=new Thread(){
String line;
public void run() {
try {
while (true) {
outputStream.write( (reader.readLine()+"\n").getBytes());
outputStream.flush();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
};
}
public void startIn(){ //start thread tIn
if(tIn!=null){
tIn.start();
}
}
public void startErr(){ //start thread tErr
if(tErr!=null){
tErr.start();
}
}
public void startOut(){ //start thread tOut
if(tOut!=null){
tOut.start();
}
}
public void interruptIn(){ //interrupt thread tIn
if(tIn!=null){
tIn.interrupt();
}
}
public void interruptErr(){ //interrupt thread tErr
if(tErr!=null){
tErr.interrupt();
}
}
public void interruptOut(){ //interrupt thread tOut
if(tOut!=null){
tOut.interrupt();
}
}
}
CODE: mainC.java
----------------------------------------------------------------------------------
public final class mainC {
public static void main(String[] args) {
jShell shell=new jShell("ls -l");
shell.startErr();
shell.startIn();
shell.startOut();
}
}
RESULT:
---------------------------------------------------------------------------------
0. 总用量 44
1. -rwxrwxrwx 1 root root 219 5月 12 10:41 ex.pl
2. -rwxrwxrwx 1 root root 211 5月 12 10:39 ex.pl~
3. -rwxrwxrwx 1 root root 150 5月 12 10:41 ex.sh
4. -rwxrwxrwx 1 root root 124 5月 12 10:20 ex.sh~
5. -rwxrwxrwx 1 root root 1198 5月 12 10:43 jShell$1.class
6. -rwxrwxrwx 1 root root 1198 5月 12 10:43 jShell$2.class
7. -rwxrwxrwx 1 root root 1222 5月 12 10:43 jShell$3.class
8. -rwxrwxrwx 1 root root 2241 5月 12 10:43 jShell.class
9. -rwxrwxrwx 1 root root 2720 5月 12 10:43 jShell.java
10. -rwxrwxrwx 1 root root 544 5月 12 11:43 mainC.class
11. -rwxrwxrwx 1 root root 170 5月 12 11:43 mainC.java
PS:
Process process=Runtime.getRuntime().exec("");中产生停滞(阻塞,blocking),怎么解决?
---------------------------------------------------------------
这个是因为Runtime.getRuntime().exec()要自己去处理stdout和stderr的。
所以如果你想让程序正常运行的话,请务必将上述用别的线程流取走。
【JAVA中执行bat】
>test.bat
haha
exit 99
>RuntimeTest.java
public class RuntimeTest {
public static void main(String[] args) {
try {
Process process=Runtime.getRuntime().exec("test.bat");
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
// kick off stderr
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
// kick off stdout
outGobbler.start();
process.waitFor();
System.out.println(process.exitValue());
} catch(Exception e) {}
}
}
>StreamGobbler.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
try {
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
发表评论
-
JIRA REST API ---- JAVA
2015-09-24 15:51 13291最近在搞自动化监控 ... -
Thrift--JSClient
2013-09-26 14:45 5933thrift提供了基于jquery--ajax的客户端调用 ... -
Thrift--Spring集成ThriftServlet
2013-09-25 11:42 11090Thrift除了可以通过TCP协议访问,还可以通过HTTP ... -
Thrift转SpringHttpInvoker
2013-09-24 13:26 1740关于在spring中集成Thrift请参看:http://h ... -
Thrift--JavaServer&PythonClient遇到的问题
2013-09-23 10:16 1412thrift在java中的应用参考:http://hanqu ... -
Spring集成Thrift--Server AND Client
2013-09-04 20:13 13726Thrift网上有N多教程, ... -
Windows Server 2008 Active Directory 安装及使用笔记
2013-03-22 18:49 26431.安装AD http://www.docin.com/ ... -
C3P0配置实战
2012-09-04 18:34 51855C3P0: 一个开源的JDBC连接池,它实现了数据源和JN ... -
使用Spring3.1 Cache注解+EHCache遇到的问题
2011-10-20 17:48 10339关于Spring3.1 Cache注解的介绍请参看http:/ ... -
JAVA压缩与解压缩--zip
2011-03-03 16:52 3383通过java打zip包或者解压 ... -
java调用Shell脚本
2009-07-10 13:13 2273原文地址:http://hi.baidu.com/qiu115 ... -
JDK5 Annotation(注释)的用法
2009-07-10 13:27 1783原文地址:http://hi.baidu.com/gaoyao ... -
JDK 1.5中的ENUM用法
2009-07-10 13:30 1284原文地址:http://www.cnblogs.com/jac ... -
java反射介绍
2009-07-10 14:31 1086一。课程:检查类 1.获得Class对象 得到 ... -
jdk1.5泛型介绍
2009-07-11 09:42 1085原文地址:http://www.matrix.org.cn/r ... -
中文与acsii码相互转换方法
2009-07-13 17:59 1411在开发时,经常会用到 ... -
巧用系统属性
2009-07-15 11:22 1873我们都曾在项目中使用 ... -
String与InputStream相互转换
2009-07-20 18:48 17041.String to InputStream String ... -
java.util.Date 与java.sql.Date相互转换
2009-07-21 10:57 16741.java.util.Date to java.sql.Da ... -
java验证日期格式
2009-07-24 21:02 1709public static boolean checkDate ...
相关推荐
Microsoft.WindowsAPICodePack.Shell.dll
shell,bat脚本运行java程序, shell,bat脚本运行java程序,
在装jenkins 服务器下放了所有的脚本,开发环境,测试环境,预发环境,生产环境的脚本,在jenkins中构建项目,执行本地脚本来启动远程java项目,重点:脚本都在jenkins这台服务器进行管理。
android13 WindowManager-Shell.jar
这是我在韩国留学期间和小组成员一同完成的一套shell,具备创建文件和发送邮件等功能,在linux的打开终端里打开的.sh文件
java调用shell命令.pdf
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Shell\2.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Shell.dll
#上传至目录后,进入脚本所在目录执行命令 chmod +x start.sh #脚本使用: sh start.sh start sh start.sh stop sh start.sh restart sh start.sh status 或者: ./start.sh start ./start.sh stop ./start.sh ...
批量主机远程执行命令脚本.sh 批量创建100用户并设置密码脚本.sh 批量检测网站是否异常脚本.sh 找出占用CPU 内存过高的进程脚本.sh 更多精品教程.url 服务器系统配置初始化脚本.sh 本教程由我爱学it提供.url ...
crash, Java平台的shell ______. ~ ~. |`````````,. '.. . '''' | || |'''|'''''. ''```.. '' |_________|| | `.. ' `.. . ' | | `.___
java执行shell或bat脚本,Java可以使用三种方式来执行.bat或.shell脚本文件:使用Runtime.exec()、使用ProcessBuilder、使用第三方工具包commonsexec.jar
Java实现远程执行SHELL, JAVA实现远程登陆执行shell脚本。上传文件到远程服务器,下载远程服务器资源。 ShellExecutor exe = new ShellExecutor("192.168.1.1", 22, "USERNAME", "PASSWORD"); if (exe.login()) {...
shell.sh
短信验证码注册登录Shell 的两种方法- Httpie.sh、Shell - wget.sh文件.zip
java连接linux,执行shell命令的工具类 sshxcute.jar
Android中级篇之用JAVA代码执行shell命令
liunx 服务器shell 监控进程号的脚本,服务挂掉后,可执行重启服务。但前提需要将该脚本放到定时任务中; 如:* * * * * sh /home/kaiyuanuser/webspace/shell/auto_process.sh * * * * * sleep 15; sh /home/...