请指教!!编程出错!!请大虾指点!PING的问题!! 大哥,肯定是没有编译好的Ping class ,检查你运行时的命令行有没有错啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看下面的程序,我已经测试过,No problem!import java.io.*;import java.util.*;import java.net.*;public class Ping { public static void main(String[] args){ if(args.length!=1) {System.out.println("Usage:java Ping hostname"); System.exit(1); } //Get the input hostname. String hostname=args[0]; //DNS try{ InetAddress addr=InetAddress.getByName(hostname); hostname=addr.getHostAddress(); }catch(UnknownHostException ex) { System.out.println("Unknown host:"+hostname); System.exit(1); } //invoke the ping.exe try{ Runtime r=Runtime.getRuntime(); //Get the system property "Path",and set the envirenment of the process. Properties prop=System.getProperties(); String path=prop.getProperty("java.library.path"); String[] envp=new String[1]; envp[0]="path="+path; //ping command and its arguments. String[] cmd=new String[2]; cmd[0]="ping"; cmd[1]=hostname; //execute it. Process p=r.exec(cmd,envp); BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream())); if(in!=null){ boolean finished=false; while(!finished){ String s=in.readLine(); if(s!=null) { if(!s.trim().equalsIgnoreCase("")) System.out.println(s); } else finished=true; }//while }//if //wait for the process to end. int exitcode=p.waitFor(); //System.out.println("ExitCode:"+exitcode); }catch(Exception ex) {System.out.println("Exception message:"+ex.getMessage()); ex.printStackTrace(); }//catch }} 程序名是ping or ping2 ?请在执行前加上路径参数 -cp 试过了,但是只能ping4次就自动断开连接了,怎么才能使他连续ping?谢谢 上次实现的是不待参数的ping.稍作修改就课和实际的ping完全一样。要连续ping的格式:java -cp ./ Ping -t www.csdn.net代码如下:import java.io.*;import java.util.*;import java.net.*;public class Ping { public static void main(String[] args){ int len=args.length; if(len<1) {System.out.println("Usage:java Ping [options] hostname"); System.exit(1); } //Get the input hostname. String hostname=args[len-1]; //DNS try{ InetAddress addr=InetAddress.getByName(hostname); hostname=addr.getHostAddress(); }catch(UnknownHostException ex) { System.out.println("Unknown host:"+hostname); System.exit(1); } //invoke the ping.exe try{ Runtime r=Runtime.getRuntime(); //Get the system property "Path",and set the envirenment of the process. Properties prop=System.getProperties(); String path=prop.getProperty("java.library.path"); String[] envp=new String[1]; envp[0]="path="+path; //ping command and its arguments. String[] cmd=new String[len+1]; cmd[0]="ping"; cmd[len]=hostname; /********新加的***************/ for(int i=1;i<len;i++) cmd[i]=args[i-1]; //execute it. /************************/ Process p=r.exec(cmd,envp); BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream())); if(in!=null){ boolean finished=false; while(!finished){ String s=in.readLine(); if(s!=null) { if(!s.trim().equalsIgnoreCase("")) System.out.println(s); } else finished=true; }//while }//if //wait for the process to end. int exitcode=p.waitFor(); //System.out.println("ExitCode:"+exitcode); }catch(Exception ex) {System.out.println("Exception message:"+ex.getMessage()); ex.printStackTrace(); }//catch }} 一个小小的疑问 刚学数组,请问降序排列输出如何编写? 小菜鸟的程序看不懂,请大虾过来看看!!一定给分~~~!!!! 一个类似敲地鼠的小游戏,线程部分有问题,请高手指教 如何立即显示矩形(在线等待) 怎么让弹出的dialog显示在屏幕中间呢? Weblogic console 无法启动 文件拷贝问题 用JAVA编一个程序,测试网络速度~~高手帮忙 -------%%%%%%% WEB目录问题 %%%%%%%----- 布局管理器的简单问题 菜鸟级问题!!
import java.io.*;
import java.util.*;
import java.net.*;
public class Ping { public static void main(String[] args){
if(args.length!=1)
{System.out.println("Usage:java Ping hostname");
System.exit(1);
}
//Get the input hostname.
String hostname=args[0];
//DNS
try{
InetAddress addr=InetAddress.getByName(hostname);
hostname=addr.getHostAddress();
}catch(UnknownHostException ex)
{ System.out.println("Unknown host:"+hostname);
System.exit(1);
} //invoke the ping.exe
try{
Runtime r=Runtime.getRuntime();
//Get the system property "Path",and set the envirenment of the process.
Properties prop=System.getProperties();
String path=prop.getProperty("java.library.path");
String[] envp=new String[1];
envp[0]="path="+path;
//ping command and its arguments.
String[] cmd=new String[2];
cmd[0]="ping";
cmd[1]=hostname;
//execute it.
Process p=r.exec(cmd,envp); BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
if(in!=null){
boolean finished=false;
while(!finished){
String s=in.readLine();
if(s!=null)
{ if(!s.trim().equalsIgnoreCase(""))
System.out.println(s);
}
else
finished=true;
}//while
}//if
//wait for the process to end.
int exitcode=p.waitFor();
//System.out.println("ExitCode:"+exitcode);
}catch(Exception ex)
{System.out.println("Exception message:"+ex.getMessage());
ex.printStackTrace();
}//catch
}
}
请在执行前加上路径参数 -cp
谢谢
代码如下:
import java.io.*;
import java.util.*;
import java.net.*;
public class Ping { public static void main(String[] args){
int len=args.length;
if(len<1)
{System.out.println("Usage:java Ping [options] hostname");
System.exit(1);
}
//Get the input hostname.
String hostname=args[len-1];
//DNS
try{
InetAddress addr=InetAddress.getByName(hostname);
hostname=addr.getHostAddress();
}catch(UnknownHostException ex)
{ System.out.println("Unknown host:"+hostname);
System.exit(1);
} //invoke the ping.exe
try{
Runtime r=Runtime.getRuntime();
//Get the system property "Path",and set the envirenment of the process.
Properties prop=System.getProperties();
String path=prop.getProperty("java.library.path");
String[] envp=new String[1];
envp[0]="path="+path;
//ping command and its arguments.
String[] cmd=new String[len+1];
cmd[0]="ping";
cmd[len]=hostname;
/********新加的***************/
for(int i=1;i<len;i++)
cmd[i]=args[i-1];
//execute it.
/************************/
Process p=r.exec(cmd,envp); BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
if(in!=null){
boolean finished=false;
while(!finished){
String s=in.readLine();
if(s!=null)
{ if(!s.trim().equalsIgnoreCase(""))
System.out.println(s);
}
else
finished=true;
}//while
}//if
//wait for the process to end.
int exitcode=p.waitFor();
//System.out.println("ExitCode:"+exitcode);
}catch(Exception ex)
{System.out.println("Exception message:"+ex.getMessage());
ex.printStackTrace();
}//catch
}
}