1

Here is an overview of what I am doing. I have a Java Swing application. On it I have a button called 'Ping'. What I want to happen is that when I click on the button Ping then it should ping a particular server. The server IPAddress details are in a text box.

So this is the code I have.

try {
    Process p = Runtime.getRuntime().exec(strbf.toString());

    if (Action.equalsIgnoreCase("PING"))
    {
           BufferedReader in = new BufferedReader(  
                   new InputStreamReader(p.getInputStream()));  
           String line = null;  
           while ((line = in.readLine()) != null) 
           {  
              mylogger.logInfo("Servers", "actionToBeTaken", "Ping line is" + line);
           }
    }  

So the output of the ping goes into a logger file. Everything is good so far. But I don't want this behavior. Note that if I type in ping into the windows run prompt or dos prompt then a seperate ping window pops up. This is the way I want my code to function. Click on a button and the window automatically pops up. What changes do I make to my code? I tried different things but it does not seem to work. For example the below does not work. I don't know where the output of the ping command is getting eaten up. How do I ensure the command prompt windows pops up and the ping output is visible there.

try {
    if (Action.equalsIgnoreCase("PING"))
    {
         Runtime.getRuntime().exec(strbf.toString());
    }  
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vinay
  • 71
  • 1
  • 4
  • 1- Use `ProcessBuilder` over `Runtime.exec`. 2- Try executing `start` instead, something like `start cmd /C ping {server}` if you want the window to close automatically or may be `start cmd /K ping {server}` if you want the window to remain. A better solution would be to redirect the output to something like a `JTextArea` as demonstrated [here](http://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490) for example – MadProgrammer Oct 05 '13 at 01:14
  • start cmd /C ping {server} also does not seem to do the trick. I don't mind using a JTextArea. Thought about that in fact. But say I want to run the command ping -t then I can't use JTextArea because the command would keep executing non stop. If the command opens up it's on window, then I can terminate the program(ping.exe) by just closing the window. – Vinay Oct 05 '13 at 02:35
  • [Process#destroy](http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#destroy()) – MadProgrammer Oct 05 '13 at 02:43
  • *"..from JAVA"* There is no need to add the major tag to the title of a post, and it is spelled 'Java', not 'JAVA'. No need to 'shout it from the roof-tops'. – Andrew Thompson Oct 05 '13 at 09:08

0 Answers0