1

我正在尝试从 java 程序执行命令行。

当直接在我的 MAC 上的控制台中输入时,我已经成功地使这个命令行工作: /Applications/GIMP.app/Contents/MacOS/gimp -i -b "(let* ( ( image (car (file-png-load 1 \"/Users/Maxime/Desktop/img.png\" \"/Users/Maxime/Desktop/img.png\") ) ) (drawable (car (gimp-image-active-drawable image) ) ) ) (plug-in-colortoalpha 1 image drawable '(0 0 0) ) (gimp-file-save RUN-NONINTERACTIVE image drawable \"/Users/Maxime/Desktop/img2.png\" \"/Users/Maxime/Desktop/img2.png\") ) " -b "(gimp-quit 0)"

(我知道,很复杂,但它有效)

该命令成功使图片的黑色背景透明并保存结果。

所以我做了以下程序:

    try {
        // TODO code application logic here
        String command = "/Applications/GIMP.app/Contents/MacOS/gimp -i -b "
                + "\"(let* "
                + "("
                + "(image (car (file-png-load 1 \"/Users/Maxime/Desktop/img.png\" \"/Users/Maxime/Desktop/img.png\")))"
                + "(drawable (car (gimp-image-active-drawable image))) "
                + ")"
                + "(plug-in-colortoalpha 1 image drawable '(0 0 0))"
                + "(gimp-file-save RUN-NONINTERACTIVE image drawable \"/Users/Maxime/Desktop/img2.png\" \"/Users/Maxime/Desktop/img2.png\")"
                + ")\" -b \"(gimp-quit 0)\"";

        String command2 = "/Applications/GIMP.app/Contents/MacOS/gimp -i -b \"(gimp-quit 0)\"";

        Process pr = Runtime.getRuntime().exec(command2);

        BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        int lineCounter = 0;
        while(true)
        {
            String line = processOutput.readLine();
            if(line == null) break;
            System.out.println(++lineCounter + ": " + line);
        }

        processOutput.close();


    } catch (IOException ex) {
        Logger.getLogger(ColorToAlpha.class.getName()).log(Level.SEVERE, null, ex);
    }

不幸的是,当我使用 command2 时,输出在打印以下内容后停止:

1: gimp
2: /Applications/GIMP.app/Contents/MacOS
3: /Applications/GIMP.app/Contents/MacOS/:/Applications/GIMP.app/Contents/Resources/bin:/usr/bin:/bin:/usr/sbin:/sbin

我不知道发生了什么,因为即使使用 command2(命令的简化版本)程序也会阻塞。

如果有人知道如何做到这一点,那就太好了。

编辑:当我替换String commandString[] command添加引号时,程序成功运行,但没有任何反应

4

0 回答 0