Java运行系统命令并获取值(Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)

时间:2021-01-28 14:31:39
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.