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)的更多相关文章

  1. JAVA枚举操作(获取值,转map集合)

    JAVA枚举相对来说比.NET的枚举功能强大,感觉就像是一种简化版的类对象,可以有构造方法,可以重载,可以继承接口等等,但不能继承类,JAVA枚举在实际开发中应用相当频繁,以下几个封装方法在实际开发中 ...

  2. java运行机制、Jdk版本及Java环境变量

    一.语言特性 计算机高级语言按程序的执行方式可分为:编译型和解释型两种.编译型的语言是指使用专门的编译器,针对特定的平台(操作系统)一次性翻译成被该平台硬件执行的机器码,并包装成该平台可执行性程序文件 ...

  3. Java运行显示无待入值

    忘记添加@Param()

  4. java运行报错:nested exception is java.lang.NoSuchFieldError: INSTANCE,但使用@Test测试是好的

    解决方法: 原因是,在tomcat里,同名不同版本的jar包,默认加载版本低的.我项目里有两个httpclient jar包.一个4.2.5  另一个是4.5.所以加载了4.2.5的,而我要用的是4. ...

  5. java.lang.Runtime.exec() Payload Workarounds

    由于Runtime.getRuntime().exec()中不能使用管道符等bash需要的方法,我们需要用进行一次编码 编码工具: 地址: http://jackson.thuraisamy.me/r ...

  6. JAVA运行时异常及常见的5中RuntimeExecption

    最近在抽时间看面试题,很多面试题都提出了写出java常见的5个运行时异常.现在来总结一下, java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了两种异常机制.一种是运行时异常 ...

  7. Java改变引用数据类型的值

    Java改变引用数据类型的值 在Java中,引用数据类型的数据传递的是值(地址)的拷贝 对于以下代码 class BirthDate { private int day; private int mo ...

  8. 深入研究java.lang.Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  9. 深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

随机推荐

  1. php中的in_array分析及其替换方法

    php中的in_array函数效率分析 http://www.jb51.net/article/41446.htm http://www.server110.com/php/201309/1150.h ...

  2. 8款实用的Jquery瀑布流插件

    1.网友Null分享Jquery响应式瀑布流布局插件 首先非常感谢网友Null的无私分享,此作品是一款响应式瀑布流布局Jquery插件,网友Null增加了一个屏幕自适应和响应式,响应式就是支持智能手机 ...

  3. c#获取今天星期几

    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek)

  4. react-native-vector-icons 安装

    react-native-vector-icons 是可以直接使用图片名就能加载图片的第三方,类似于web的iconfont矢量图,使用很方便, 你不需要在工程文件夹里塞各种图片, 节省很多空间,下面 ...

  5. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

  6. YARN环境搭建 之 一:CentOS7.0系统配置

    一.我缘何选择CentOS7.0 14年7月7日17:39:42发布了CentOS 7.0.1406正式版,我曾使用过多款Linux,对于Hadoop2.X/YARN的环境配置缘何选择CentOS7. ...

  7. Sql Server 常用自定义函数

    -- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...

  8. 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题

    凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...

  9. 【iOS】屏幕旋转,屏幕自适应方向变化

    1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...

  10. Codeforces Round #352 (Div. 2) D. Robin Hood

    题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...