String ip = "www.google.ro";

		String pingResult = "";
		String pingCmd = "ping " + ip + " -c 4";
        String s;
        try {


            Process p = Runtime.getRuntime().exec(pingCmd);

            BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Rezultat ping:\n");
            

            for (int i = 0; i < 4; i++) {

                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s + "\n");
                }
            }
            
            // read any errors from the attempted command
            System.out.println("\nEventualele erori le gasiti mai jos (Any errors are below:\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s + "\n");
            }
        }
		catch (IOException e) {
			System.out.println(e);
		}

Hello,

I have a little problem with this code, This is what I have, and it's working, however, I need to output the result in a textarea.

The problem is, that it's taking too long to see any result, in the above example, println, is showing the moment ping or any command is displaying something.

When trying to output the result in the textarea, I use:

textarea.append(s + "\n")

Also, this again is working, but it's displaying all the result in one go. What I want is to append each time a ping is showing something, like the sout command.

Anyone have any suggesting on to what I can use here ?

Regards,
Bogdan

(A guess) this may be a thread problem - text area not being updated until the loop is exited. Does you code above run as part of a GUI - eg in response to a button press or somesuch?

Sorry for the late reply, it seems I didn't get the notification for your reply.

Does you code above run as part of a GUI - eg in response to a button press or somesuch?

Yes, this is correct.

There is one textarea1 to include the domain/IP and one button that will start the action (ping) and right below the textarea2 with the result.

(A guess) this may be a thread problem

Sorry, didn't really know what to choose at the start.

Regards

All Swing activity - including actionPerformed etc and screen repaints happen on a single thread - the Swing thread or Event Dispatch Thread (EDT). Google it.
If your code is started by from a button the whole Swing thing will be waiting for your method to return - but that's sitting in a loop waiting for the input to finish.
You can update Swing GUI components as much as you like. Nothing will be repainted until your button handler returns.
If you want to do anything that takes time you must put it in a new thread so it doesn't interfere with the GUI. Use your own new Thread, or use the SwingWorker class.

I understand now where the problem is. Is it ok to provide a little code to help me get started with this ?

For now I've created another test project and the panel is already set with the above code, I didn't used SwingWorker so far so this is really new.

Regards

Thank you, it seems google is a good friend for all of us. Thank you for pointing out what I needed. This is working now.

SwingWorker doPing = new SwingWorker() {
        String ip = domeniu.getText();

        String pingResult = "";
        String pingCmd = "ping " + ip + " -c 4";
        String s;
            @Override
            protected List<Boolean> doInBackground() throws Exception {
                 try {
        Process p = Runtime.getRuntime().exec(pingCmd);

        BufferedReader stdInput = new BufferedReader(new
        InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new
        InputStreamReader(p.getErrorStream()));

        // read the output from the command
        rezultat.append("Rezultat ping:\n");
        while ((s = stdInput.readLine()) != null) {
        rezultat.append(s + "\n");
        
        }
             }
        catch (IOException e) {
        System.out.println(e);
        } 
                return null;

            }


        };
        doPing.execute();

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.