Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simulating PING command

807569Sep 22 2006 — edited Sep 22 2006
Hello everyone,

Ping command is same in all the operating systems so I am simulating the ping command as follows:-
 
import java.util.*;

public class PingDemo {

public void ping(String host) {

  try {
    Process p = Runtime.getRuntime().exec("ping "+host);
    Scanner scan = new Scanner(p.getInputStream());
    while(scan.hasNextLine()){
      System.out.println(scan.nextLine());
    }
  }catch(Exception ex){
    System.out.println("Error "+ex);
   }
}

public static void main(String args[]){
  PingDemo p=new PingDemo();
  p.ping(args[0]);
  }
}
but the output has extra line spacing, snapshot of the o/p is as follows:-

------------------------------------------------------------------------------------------------
D:\Language\Java\MyJava>java PingDemo 127.0.0.1


Pinging 127.0.0.1 with 32 bytes of data:



Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128



Ping statistics for 127.0.0.1:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms


D:\Language\Java\MyJava>

------------------------------------------------------------------------------------------------

Here if I remove System.out.println with System.out.print then the output comes in a single line which looks odd.

So, is there a way to remove those extra lines in between as it occurs in normal ping command?

Thanking in advance.

Regards,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 20 2006
Added on Sep 22 2006
6 comments
708 views