简体   繁体   中英

Opened external JAR file with JAVA ProcessBuilder does not work properly

I want to open JAR file by pressing JButton in JPanel. To achieve this goal I used ActionListener with ProcessBuilder inside. Here is my code:

myButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
    ProcessBuilder pb = new ProcessBuilder("java", "-jar"
           , "f:/Documents/TBot/topbotclient.jar"
           , "-n", getTopBotName().getText()
           , "-pw", getTopBotPass().getText()
           , "-s", getScript_name().getText()
           , "-w", getWorld().getText()
         );
    try {
        Process p = pb.start();
    } catch (IOException ee) {
        ee.printStackTrace();
    }

        }
    });

The problem is that opened JAR file do not function properly - it freezes after I press some of its buttons. However, if I close my initial JAVA window that is used to open external JAR, JAR file becomes functional again. What to do to get both initial window and opened JAR file window functional?

I did not find any solution by searching: Run a jar File from java program , Execute .jar file from a Java program etc.

UPDATE:

I tried not to use ProcessBuilder and used "runtime.exec" instead.

try {
    Runtime runtime = Runtime.getRuntime();
    runtime.exec(" java -jar f:/Documents/TBot/scripts/topbot.jar -n Fataho -pw diehard15 -s scriptjoiner -w 301 -nort -a b@hadas.lt -apw blood444");
} catch (Exception ex) {
    JOptionPane.showMessageDialog(null, "Exception occured" + ex);
}

The problem remains the same.

Try to start a processbuilder in a separate thread so that your main thread won't block. From your code your doing everything in a Main thread. Make use of swingWorker kind of stuff to start the operation in a separate thread.

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