java+netbeans编一个ping的小程序,请高手指教 java+netbeans编一个ping的小程序,请高手指教!要求程序能够类似ping的命令ping网络,还能使用ipconfig,tracert,等命令,可以针对不同的ip定时定制任务 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考代码:public class Test{/* *执行命令 *@param cmd 要执行的命令 * * */public static void execCmd(String cmd) throws IOException { Process p=Runtime.getRuntime().exec("cmd /c "+cmd); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); String line=null; while((line=br.readLine())!=null) { System.out.println(line); } br.close();}public static void main(String[] args)throws Exception { execCmd("ipconfig"); } }程序执行结果:Windows IP ConfigurationEthernet adapter 本地连接: Media State . . . . . . . . . . . : Media disconnectedEthernet adapter 内部无线网卡: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.1.187 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 试了,很好用,谢谢。不过这句是什么意思?BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));这个是把输入流读到字节缓冲区中,主要是为了提高性能,也可用不用,直接用InputStreamReader。以下是我以前看到的解决办法:String[] cmd = new String[5]; cmd[0] = "cmd"; cmd[1] = "/c"; cmd[2] = "start"; cmd[3] = " "; cmd[4] = filePath; Process process = Runtime.getRuntime().exec(cmd); String[] cmd = new String[5];cmd[0] = "cmd";cmd[1] = "/c";cmd[2] = "start";cmd[3] = " ";cmd[4] = filePath;Process process = Runtime.getRuntime().exec(cmd);或; Java代码 Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\""); Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");因为按找文档说明 start 命令之后首先是[title],再是[filepath],所以将title设置为 " " 调用外部程序来打开一个相应的文件 比如我们要使用Editplus来打开一个远程机器上的文件,可以这样 Java代码 Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即 Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件"). Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").以此方式可以忽略空格的问题 截取控制台的信息. 使用JAVA输入流的方式. Java代码 Runtime.getRuntime().exec(..)获取的Process; Process pro = Runtime.getRuntime().exec(..); InputStreamReader isr = new InputStreamReader (pro.getInputStream()); //todo 使用输入流进行操作就可以了. Runtime.getRuntime().exec(..)获取的Process;Process pro = Runtime.getRuntime().exec(..);InputStreamReader isr = new InputStreamReader (pro.getInputStream());//todo 使用输入流进行操作就可以了.另外JDK6也提供了一种方式 Java代码 Desktop dsk=DeskTop.getDesktop(); dsk.open(new File(filePath)); 程序只是直接用JAVA调用 WINDOWS 的ipconfig命令。。并非用JAVA实现获取IP信息。。 求jsp实现网页录音功能 java菜鸟的烦恼 如何用swing实现图中对文本框的输入提示效果? 问个经典的算法。。大家来动动脑筋 如何判断输入的内容是"2009-03-12 21:03:01"格式 求助,请高手帮我分析一下这个程序,谢谢! str .append(ch) 中append 是什么用的 JAVA,我真的后悔没学..... 本人所在小公司想做一个网站.请问一下用什么做服务器?用java tree 怎么得到鼠标右键事件啊!一定马上就给40分 关于异常程序,不太理解 一道JAVA面试题 关于ZIP压缩
/*
*执行命令
*@param cmd 要执行的命令
*
* */
public static void execCmd(String cmd) throws IOException
{
Process p=Runtime.getRuntime().exec("cmd /c "+cmd);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=null;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
br.close();
}public static void main(String[] args)throws Exception {
execCmd("ipconfig");
} }
程序执行结果:
Windows IP ConfigurationEthernet adapter 本地连接: Media State . . . . . . . . . . . : Media disconnectedEthernet adapter 内部无线网卡: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.1.187 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1
不过这句是什么意思?
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
这个是把输入流读到字节缓冲区中,主要是为了提高性能,也可用不用,直接用InputStreamReader。
以下是我以前看到的解决办法:String[] cmd = new String[5];
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = filePath;
Process process = Runtime.getRuntime().exec(cmd);
String[] cmd = new String[5];
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = filePath;
Process process = Runtime.getRuntime().exec(cmd);
或;
Java代码
Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");
Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");
因为按找文档说明 start 命令之后首先是[title],再是[filepath],所以将title设置为 " "
调用外部程序来打开一个相应的文件
比如我们要使用Editplus来打开一个远程机器上的文件,可以这样
Java代码
Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即
Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").
Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即
Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").以此方式可以忽略空格的问题 截取控制台的信息.
使用JAVA输入流的方式. Java代码
Runtime.getRuntime().exec(..)获取的Process;
Process pro = Runtime.getRuntime().exec(..);
InputStreamReader isr = new InputStreamReader (pro.getInputStream());
//todo 使用输入流进行操作就可以了.
Runtime.getRuntime().exec(..)获取的Process;
Process pro = Runtime.getRuntime().exec(..);
InputStreamReader isr = new InputStreamReader (pro.getInputStream());
//todo 使用输入流进行操作就可以了.另外JDK6也提供了一种方式 Java代码
Desktop dsk=DeskTop.getDesktop();
dsk.open(new File(filePath));