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.

 

转载于:https://www.cnblogs.com/IamThat/p/4359130.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值