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对象, ...
随机推荐
- 关于Silverlight调用天气预报接口问题
问题:因Silverlight客户端不能直接调用webservice接口(外网天气接口),调用会出现跨域访问的问题,即使添加了跨域文件也不好使.解决方法如下 解决方法一:1.在服务端建立一个wcf服务 ...
- string和json转换的简单应用
import com.alibaba.fastjson.JSON; String strjson = request.getParameter("param"); //url-js ...
- PHP+AJAX无刷新返回天气预报
AjaxJavaScript天气预报php天气预报,用php来写一个天气预报的模块. 天气数据是通过采集中国气象网站的.本来中国天气网站也给出了数据的API接口.以下是API的地址.返回的数据格式为j ...
- JavaScript创建对象的写法
JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发者自定义对象. 对象是什么 从 ...
- ActiveMQ之selector的用法
前面的例子中创建一个消息消费者使用的是: sesssion.createConsumer(destination) 另外,还提供了另一种方式: sesssion.createConsumer(dest ...
- python之函数嵌套
python很多特性与JavaScript是相似甚至相同的: 1. 无类型 2. 函数亦对象 .... 自然: python也允许函数嵌套, 这与JavaScript中函数闭包的作用一样....
- centos crontab 定时任务详解
安装crontab: yum install crontabs 说明: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 ...
- Unity 优化
1. 尽量避免每帧处理比如: function Update() { DoSomeThing(); } 可改为每5帧处理一次: function Update() { == ) { DoSomeThi ...
- php获取指定日期所在星期的开始时间与结束时间
function getWeekRange($date){ $ret=array(); $timestamp=strtotime($date); $w=strftime('%u',$ ...
- C# 越来越复杂了
自从三年前来到现在的公司以后,基本上不怎么使用.NET进行开发了.但最近因为公司有个CRM的项目,所以只有重新检起.NET进行开发. 因为近3年没有搞.NET的开发了,因此也不敢乱整个框架,在看了一周 ...