java调用系统命令
2009-10-05 21:27
411 查看
由于java 是跨平台的,有时候我们需要用到操作系统的一些信息,为了方便期间,干脆就直接调用操作系统的命令来实现,比如查看IP地址,MAC地址等。不过两个在jdk6里面已经有了,不过以前都是用调用dos命令,然后获取输出的办法来做的,比如:
Java代码
import java.io.*;
public class DT {
public static void main(String[] args) throws IOException
{
String command="ipconfig";<BR> Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("/n");
}
System.out.println(sb.toString());
}
}
这样的话可以得到如下的结果:
结果代码
Windows IP Configuration
Ethernet adapter VMware Network Adapter VMnet8:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.79.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.23.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.100.12
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.100.250
然而,当我在执行wmic 命令的时候,想要获取它的输出确不行了,看代码:
Java代码
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ManagerArgs {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程序在执行的时候就柱塞了,根本获取不到想ipconfig 那样的结果,把命令放到DOS 命令里面取执行,看图片:
这里确实可以的。难道wmic 命令的结果java 就取不到了吗?同样的都是dos 命令,为啥就有这样的区别呢?
Java代码
import java.io.*;
public class DT {
public static void main(String[] args) throws IOException
{
String command="ipconfig";<BR> Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("/n");
}
System.out.println(sb.toString());
}
}
import java.io.*; public class DT { public static void main(String[] args) throws IOException { String command="ipconfig"; Runtime r=Runtime.getRuntime(); Process p=r.exec(command); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuffer sb=new StringBuffer(); String inline; while(null!=(inline=br.readLine())){ sb.append(inline).append("/n"); } System.out.println(sb.toString()); } }
这样的话可以得到如下的结果:
结果代码
Windows IP Configuration
Ethernet adapter VMware Network Adapter VMnet8:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.79.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.23.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.100.12
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.100.250
Windows IP Configuration Ethernet adapter VMware Network Adapter VMnet8: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.79.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter VMware Network Adapter VMnet1: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.23.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter 本地连接: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.100.12 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.100.250
然而,当我在执行wmic 命令的时候,想要获取它的输出确不行了,看代码:
Java代码
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ManagerArgs {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ManagerArgs { public static void main(String[] args) { try { Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath"); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
程序在执行的时候就柱塞了,根本获取不到想ipconfig 那样的结果,把命令放到DOS 命令里面取执行,看图片:
这里确实可以的。难道wmic 命令的结果java 就取不到了吗?同样的都是dos 命令,为啥就有这样的区别呢?
相关文章推荐
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值
- oracle 调用java执行系统命令(linux环境)
- JAVA调用系统命令或可执行程序
- java调用系统命令之oracle数据库备份
- java 调用系统命令获MAC、 解压缩 tar包
- JAVA调用系统命令或可执行文件
- Java调用系统命令学习(一)
- Java基础之调用系统命令或可执行程序
- Java调用执行Linux系统命令
- Java 调用系统命令 Runtime Process ProcessBuilder
- JAVA调用系统命令或可执行程序
- Java调用系统命令学习(2)
- java调用系统命令Demo
- Java利用ImageMagick调用linux系统命令实现Ai或者PSD文件转化为jpg或者png文件
- Java调用R(三)_系统命令调用
- Java调用系统命令学习(3)
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- java调用系统命令
- Java通过Runtime类对系统命令的调用,备份数据库和图片示例
- 【Java】如何调用系统命令