Dork Face Dork Face -3 years ago 147
Java Question

Java not executing commands in Linux

I'm making a GUI for a CLI program and when this event is activated the

text instantly changes to
Task: Finished Exporting

Why is the
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 + "\"");
jLabel4.setText("Task: Exporting...");
} 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"

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download