0
public class Actualizacion extends SwingWorker<Void, String>{
    
    private final String cmd;
    private final JTextArea jTextArea1;
    
    public Actualizacion (String c, JTextArea j){
        
        cmd = c;
        jTextArea1 = j;
        
    }

    @Override
    protected Void doInBackground() throws Exception {
        Process powerShellProcess = Runtime.getRuntime().exec(cmd);
                        // Getting the results
        powerShellProcess.getOutputStream().close();
        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(
                powerShellProcess.getInputStream()));

        while ((line = stdout.readLine()) != null) {
            publish(line + "\n");
            //System.out.println("" + line);
        }

        stdout.close();

        BufferedReader stderr = new BufferedReader(new InputStreamReader(
                powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            publish(line + "\n");
            //System.out.println("" + line);
        }
        stderr.close();
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    protected void done(){
        
        
        publish("\nDone");
        try {
            get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        
        
    }
    
    @Override
    protected void process(List<String> lines){
        
       lines.forEach((String line) -> {
       
           jTextArea1.append(line);
       
       });
        
    }
    
}

This is my SwingWorker class which is supposed to update in real time a jTextArea with the output of the command "cmd" so as i said The output should be written in real time but it happens at the end of the command execution and i get an exception too.

I've read a lot about Threads and how Java Swing has an EventDispatchThread but i'm stuck.

this is the output of the program execution:


run:
java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Not supported yet.
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at javax.swing.SwingWorker.get(SwingWorker.java:602)
    at destreamer.Actualizacion.done(Actualizacion.java:66)
    at javax.swing.SwingWorker$5.run(SwingWorker.java:737)
    at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.run(SwingWorker.java:832)
    at sun.swing.AccumulativeRunnable.run(AccumulativeRunnable.java:112)
    at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.actionPerformed(SwingWorker.java:842)
    at javax.swing.Timer.fireActionPerformed(Timer.java:313)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    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:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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.lang.UnsupportedOperationException: Not supported yet.
    at destreamer.Actualizacion.doInBackground(Actualizacion.java:57)
    at destreamer.Actualizacion.doInBackground(Actualizacion.java:22)
    at javax.swing.SwingWorker$1.call(SwingWorker.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at javax.swing.SwingWorker.run(SwingWorker.java:334)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Thank you so much!

Sexde
  • 13
  • 3
  • 1
    May be start by removing the line `throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.` – MadProgrammer Jan 12 '21 at 22:11
  • So, one of qwerks of `SwingWorker` is that `process` needs to find time to run. So, if you call `publish` a lot, in very short succession, `process` won't be called, until there is a sufficient enough delay. You might try adding a `Thread.yield` or `Thread.sleep` in after process and see what happens – MadProgrammer Jan 12 '21 at 22:13
  • I'd also recommend `ProcessBuilder` over simply `Runtime.getRuntime().exec`, it will help solve a bunch of other issues – MadProgrammer Jan 12 '21 at 22:14
  • The readline() methood will block until there is data to read. You should get output displayed in the text area at roughly the same time as you get the data displayed with your System.out.println(). Post an [mre] demonstrating the problem. – camickr Jan 12 '21 at 22:16
  • For [example](https://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490) and [example](https://stackoverflow.com/questions/34858405/how-can-i-make-this-method-update-the-gui-within-my-loop/34864911#34864911) – MadProgrammer Jan 12 '21 at 22:18
  • 1
    Ok guys ... I've just tried to "adapt" the last examples which @MadProgrammer post in the last comment and it works. now i'm trying to figure out how hahaha.. Thank you so much!!!!! You're amazing guys! – Sexde Jan 12 '21 at 23:04

1 Answers1

0

This is the solution of my problem:

First i changed Runtime.getRuntime().exec to ProcessBuilder.

Then i realized that i was executing an horrible loop inside the UI so that's the main reason why the Swing Worker thread was executing after the EventDispatchThread.

Don't use wild loops inside the UI guys, in my case was:

while (!file.exist()){

}

i know this error could be a basic one but i hope this post could help you in the future.

Thanks to @MadProgrammer and @camickr for answer me that fast.

Sexde
  • 13
  • 3