简体   繁体   中英

SwingWorker publish() not working properly

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM