0

I'm making a GUI for a CLI program and when this event is activated the jLabel4 text instantly changes to Task: Finished Exporting.

Why is the .exec(...); command not working? It's not the command syntax, I tried replacing my command with touch new.file and that doesn't work either.

To me it seems like it doesn't event try to execute the command.

Java Code:

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (ext[1].equals("iso") || ext[1].equals("wbfs")) {
        String tmpPath = "";
        if (jtPath.indexOf(".") > 0)
             tmpPath = jtPath.substring(0, jtPath.lastIndexOf("."));

        Process p;
        try {
            p = Runtime.getRuntime().exec("wit extract \"" + jtPath + "\" \"" + tmpPath + "\"");
            p.waitFor();
            jLabel4.setText("Task: Exporting...");
            p.destroy();
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("wit extract \"" + jtPath + "\" \"" + tmpPath + "\"");

        jLabel4.setText("Task: Finished Exporting");
    } else {
        JOptionPane.showMessageDialog(null, "You can only extract .iso and .wbfs file formats.");
    }
}    

Command Line Output:

wit extract "/home/adam/Wii Hacking/NSMBW SMNE01.wbfs" "/home/adam/Wii Hacking/NSMBW SMNE01"
5
  • 2
    For one, you're not handling the process's streams at all, risking you running out of buffer space and not recognizing command line error messages. For another your code runs afoul of Swing threading rules. Commented Aug 16, 2017 at 16:38
  • Also, you need to capture and evaluate the int returned from p.waitFor();. Commented Aug 16, 2017 at 16:49
  • @HovercraftFullOfEels Exit value is 27, don't know what that means. Commented Aug 16, 2017 at 16:54
  • It should exit with 0. Check the wit documentation to see what this value means. And do handle the processes InputStream and ErrorStream to see if any useful information is returned and to prevent running out of buffer memory. Commented Aug 16, 2017 at 16:56
  • @HovercraftFullOfEels Error 27 is "Can't open file" but when I execute the same command from the terminal instead of java it works fine. Commented Aug 16, 2017 at 17:03

1 Answer 1

2

Probably the problem is the space in the file name or, more exactly, that quotes do not work in the command string since it is not processed by a shell.

Try using the exec method that accepts an array of strings:

p = Runtime.getRuntime().exec(new String[] {"wit", "extract", jtPath, tmpPath});

And still do handle standard output and error.

Sign up to request clarification or add additional context in comments.

1 Comment

Also have a look at java.lang.ProcessBuilder - it is a bit newer and has some nice utility methods for redirecting input/output.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.