Java运行系统命令并获取值(Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)
package test; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import javax.swing.JDialog; public class RuntimeCMD { private static Process p; public static void main(String[] args) throws IOException, InterruptedException {
InputStream in=getInputStream("java -version");
BufferedReader fr=new BufferedReader(new InputStreamReader(in));
String line;
while((line=fr.readLine())!=null){
System.out.println(line);
}
p.waitFor();
fr.close();
p.destroy();
} private static InputStream getInputStream(String exec) throws IOException{
p=Runtime.getRuntime().exec(exec);
return p.getInputStream();
}
} result: 控制台空 package test; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import javax.swing.JDialog; public class RuntimeCMD { private static Process p; public static void main(String[] args) throws IOException, InterruptedException {
InputStream in=getInputStream("ping www.baidu.com");
BufferedReader fr=new BufferedReader(new InputStreamReader(in));
String line;
while((line=fr.readLine())!=null){
System.out.println(line);
}
p.waitFor();
fr.close();
p.destroy();
} private static InputStream getInputStream(String exec) throws IOException{
p=Runtime.getRuntime().exec(exec);
return p.getInputStream();
}
} 控制台如下:
PING www.a.shifen.com (180.97.33.107): 56 data bytes
64 bytes from 180.97.33.107: icmp_seq=0 ttl=53 time=27.936 ms
64 bytes from 180.97.33.107: icmp_seq=1 ttl=53 time=29.348 ms
64 bytes from 180.97.33.107: icmp_seq=2 ttl=53 time=30.047 ms
64 bytes from 180.97.33.107: icmp_seq=3 ttl=53 time=29.287 ms
64 bytes from 180.97.33.107: icmp_seq=4 ttl=53 time=28.079 ms
64 bytes from 180.97.33.107: icmp_seq=5 ttl=53 time=30.509 ms
64 bytes from 180.97.33.107: icmp_seq=6 ttl=53 time=31.088 ms
64 bytes from 180.97.33.107: icmp_seq=7 ttl=53 time=31.085 ms
64 bytes from 180.97.33.107: icmp_seq=8 ttl=53 time=31.039 ms
64 bytes from 180.97.33.107: icmp_seq=9 ttl=53 time=28.541 ms
64 bytes from 180.97.33.107: icmp_seq=10 ttl=53 time=32.813 ms
64 bytes from 180.97.33.107: icmp_seq=11 ttl=53 time=30.702 ms
64 bytes from 180.97.33.107: icmp_seq=12 ttl=53 time=30.639 ms
64 bytes from 180.97.33.107: icmp_seq=13 ttl=53 time=33.224 ms
64 bytes from 180.97.33.107: icmp_seq=14 ttl=53 time=29.584 ms
64 bytes from 180.97.33.107: icmp_seq=15 ttl=53 time=29.465 ms
64 bytes from 180.97.33.107: icmp_seq=16 ttl=53 time=30.852 ms
64 bytes from 180.97.33.107: icmp_seq=17 ttl=53 time=31.766 ms
64 bytes from 180.97.33.107: icmp_seq=18 ttl=53 time=30.930 ms
Runtime.exec() 不等同于直接执行command line命令!
Runtime.exec()很有局限性,对有些命令不能直接把command line里的内容当作String参数传给exec().
比如重定向等命令。举个例子:
javap -l xxx > output.txt
这时要用到exec的第二种重载,即input 参数为String[]:
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","javap -l xxx > output.txt"});
没能明白 在runtime下调用java -version 为什么没有输出,但是用其他的语言就可以有输出了。
如
#!/bin/bash
perl -version
package test; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; public class RuntimeCMD { private static Process p; public static void main(String[] args) throws IOException, InterruptedException {
InputStream in=getInputStreambyExecScript("scripts/test.sh");
BufferedReader fr=new BufferedReader(new InputStreamReader(in));
String line;
while((line=fr.readLine())!=null){
System.out.println(line);
}
p.waitFor();
fr.close();
p.destroy();
} private static InputStream getInputStream(String exec) throws IOException{
p=Runtime.getRuntime().exec(exec);
return p.getInputStream();
} private static InputStream getInputStreambyExecScript(String exec) throws IOException{
p=Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",exec},null,null);
return p.getInputStream();
}
}
控制台正常输出
This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail) Copyright 1987-2013, Larry Wall Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
Java运行系统命令并获取值(Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)的更多相关文章
- JAVA枚举操作(获取值,转map集合)
JAVA枚举相对来说比.NET的枚举功能强大,感觉就像是一种简化版的类对象,可以有构造方法,可以重载,可以继承接口等等,但不能继承类,JAVA枚举在实际开发中应用相当频繁,以下几个封装方法在实际开发中 ...
- java运行机制、Jdk版本及Java环境变量
一.语言特性 计算机高级语言按程序的执行方式可分为:编译型和解释型两种.编译型的语言是指使用专门的编译器,针对特定的平台(操作系统)一次性翻译成被该平台硬件执行的机器码,并包装成该平台可执行性程序文件 ...
- Java运行显示无待入值
忘记添加@Param()
- java运行报错:nested exception is java.lang.NoSuchFieldError: INSTANCE,但使用@Test测试是好的
解决方法: 原因是,在tomcat里,同名不同版本的jar包,默认加载版本低的.我项目里有两个httpclient jar包.一个4.2.5 另一个是4.5.所以加载了4.2.5的,而我要用的是4. ...
- java.lang.Runtime.exec() Payload Workarounds
由于Runtime.getRuntime().exec()中不能使用管道符等bash需要的方法,我们需要用进行一次编码 编码工具: 地址: http://jackson.thuraisamy.me/r ...
- JAVA运行时异常及常见的5中RuntimeExecption
最近在抽时间看面试题,很多面试题都提出了写出java常见的5个运行时异常.现在来总结一下, java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了两种异常机制.一种是运行时异常 ...
- Java改变引用数据类型的值
Java改变引用数据类型的值 在Java中,引用数据类型的数据传递的是值(地址)的拷贝 对于以下代码 class BirthDate { private int day; private int mo ...
- 深入研究java.lang.Runtime类【转】
转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...
- 深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
随机推荐
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- 如何在CentOS5中增加CentALT的源
1. 建立centalt.repo 指令: vi /etc/yum.repos.d/centalt.repo 2. 將下面的內容貼進去 [CentALT] name=CentALT ...
- 我开发了一个产品--Markdown Notes
大家好,我开发了一个工具类软件产品--Markdown Notes,中文名是Markdown笔记.想写一篇有关它的文章,目的就是为了推广.推广.推广:) BTW:本文就是用这个工具所写的.
- WordPress 主题开发 - (三) 开发工具 待翻译
Before we get started building any WordPress Theme, we’re going to need to get our development tools ...
- DML,DDL,DCL,DQL的区别
DML 英文缩写 DML = Data Manipulation Language,数据操纵语言,命令使用户能够查询数据库以及操作已有数据库中的数据的计算机语言.具体是指是UPDATE更新.INS ...
- 【iOS】屏幕旋转,屏幕自适应方向变化
1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...
- bnuoj 33647 Angry Grammar Nazi(字符串)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=33647 [题意]:字符串匹配,暴力配就行了 [题解]:截出单词,然后进行匹配就行了 [code]: ...
- Careercup - Facebook面试题 - 4909367207919616
2014-05-01 01:23 题目链接 原题: WAP to modify the array such that arr[I] = arr[arr[I]]. Do this in place i ...
- [搜片神器]服务器SQL2005查询分页语句你理解了么
在sosobt.com网站准备采用Lucence.net来进行索引处理搜索慢问题的时候,突然发现常用的分页获取数据的row_number也支持不住了,后期查到200多万的时候非常慢(总数据有500万) ...
- 机器学习中的数学-矩阵奇异值分解(SVD)及其应用
转自:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/19/svd-and-applications.html 版权声明: 本文由LeftNotE ...