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. js设计模式(4)---组合模式

    0.前言 今天是建党节,新疆那边又开始了闹腾.作为立志成为码农的我,现在已经从一个大愤青淡化为一个小愤青,对这些国家民生大事不在血气喷发,转而把经历发泄在技术问题上面,因而在扯一篇随笔吧,把无处发泄的 ...

  2. 批处理cmd背景颜色

    Pause  Echo 其他提示语 & pause > nul     pause暂停 COLOR 设置默认的控制台前景和背景颜色. COLOR [attr] attr 指定控制台输出的 ...

  3. C#简单的加密类

    1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...

  4. win7 IIS 7.5 HTTP 错误 404.3 - Not Found

    HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决这个问题你只需要,打开控制面 ...

  5. 一个统计目录文件大小的php函数

    早上刚到公司,头告诉我,抓紧写一个小函数,用来统计指定目录中文件大小,我了个去,动手吧,还好有点小基础,一会就完工了,哈哈.代码在下面咯. <? /** 统计目录文件大小的函数 @author  ...

  6. SQL 查询分析器操作(修改、添加、删除)表及字段等

    一.库操作1..创建数据库命令:create database <数据库名>例如:建立一个名为xhkdb的数据库mysql> create database xhkdb; 2.显示所 ...

  7. DataSnap数据库连接池,数据集对象池的应用

    传统的应用服务器的开发往往是在ServerMethods单元中拖放一堆TDataSet, TDaTaSetProvider控件,这是一个最简单粗暴的开发方向,往往会造成服务端程序文件的臃肿.服务运行期 ...

  8. iOS 进阶 第八天(0407)

    0407 UIPickerView.UIDatePicker和UIToolBar请参见视频和代码 pch文件 #ifdef __OBJC__ //在这里面写oc的引用,比如一些oc的头文件或者NSLo ...

  9. css的display属性小实验

    div与span是常用的盒子模型; 区别: div默认是垂直分布(独占一行)   span默认是水平分布(一行可以有多个) 通过float属性可以改变div容器的分布方式达到span容器的效果; 下面 ...

  10. 从InputStream到String_写成函数

    String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...