简体   繁体   English

Java在Linux中不执行命令

[英]Java not executing commands in Linux

I'm making a GUI for a CLI program and when this event is activated the jLabel4 text instantly changes to Task: Finished Exporting . 我正在为CLI程序制作GUI,激活此事件后, jLabel4文本立即变为Task: Finished Exporting

Why is the .exec(...); 为什么.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. 这不是命令语法,我尝试用touch new.file替换命令, touch new.file也不起作用。

To me it seems like it doesn't event try to execute the command. 在我看来,尝试执行命令并非偶然。

Java Code: Java代码:

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"

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: 尝试使用接受字符串数组的exec方法:

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

And still do handle standard output and error. 并且仍然可以处理标准输出和错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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