调用系统命令 — Runtime


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
类 Runtime
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。 

应用程序不能创建自己的 Runtime 类实例。 

1, 打开文件:Runtime.getRuntime().exec("vs.net.exe");

2, 执行ping命令

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 


public class JavaPing{ 

public static void main(String[] args) {    
try {  
Process p = Runtime.getRuntime().exec("ping 192.168.1.86"); 
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  //获取执行结果
String line = "";  
while ((line = br.readLine()) != null) {  
System.out.println(line);  
}  
br.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
} 
}

注:类 Process ,进程类