0

this is my first time asking a question on these forums. I am working on a program that will ping a specific host and show ping value. Ping handler works alone, but when i try to connect with gui it is not working. Here is the code: this method searches for ping successfuly:

public static void runSystemCommand(String command) {

  int i=0;
  while (i<2){

    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader inputStream = new BufferedReader(
                new InputStreamReader(p.getInputStream()));

        String s = "";
        // reading output stream of the command
        while ((s = inputStream.readLine()) != null) {
            System.out.println(s);
        }
                    Thread.sleep(9000);

    } catch (Exception e) {
        e.printStackTrace();
    }

            i++;
}

First problem: when i want to use it on gui with start button. First of all i try with static boolean false. The code bellow will not recognize that boolean has changed to true, so it only prints Display ping: .

 private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
    if (evt.getActionCommand().equals("Display Ping")) {
  System.out.println("Display ping: ");
  GUI_COMPLETE = true;
}
}         

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new gui().setVisible(true);
            if (GUI_COMPLETE) {
            pingmain p= new pingmain();
                        String ip = "google.com";
                        p.runSystemCommand(ip);

    }
        }
    }
    );

Second problem: if i change static boolean to true, then the piece of code in gui main will work, but it gives me this error after pressing start button, so i can't ping a host through gui:

java.io.IOException: Cannot run program "google.com": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at pingviewer.pingmain.runSystemCommand(pingmain.java:25)
at pingviewer.gui$4.run(gui.java:140)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 19 more

I am sorry about length, but i can't explain it easier in my words. Any help would be much appreciated.

swipeales
  • 127
  • 1
  • 2
  • 12
  • 1
    Your `runSystemCommand` method runs a command on cmd and `google.com` is not a valid command. Try `ping google.com`. And maybe you should run this command in your `btnSubmitActionPerformed` and not in the main method. – ArcticLord Apr 04 '16 at 10:03
  • 1
    This has been asked and answered before: https://stackoverflow.com/questions/11506321/java-code-to-ping-an-ip-address – Martin Cowie Apr 04 '16 at 10:08

0 Answers0