我正在尝试从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 command
替换为String[] command
并添加引号时,程序会成功运行,但没有任何反应