java - 使用 java 执行 imagemagick 命令没有输出

标签 java command-line command imagemagick path-variables

我正在编写一个java应用程序,它使用imagemagick命令编辑图像; 但是,这些命令不起作用,我没有得到任何输出; 实际上,命令识别不被识别,我得到CreateProcess error=2; 这看起来很奇怪,因为 imagemagick 安装文件夹包含在我的 Path 变量中。

这是我的代码:

     public class Test {
    public static void main(String argv[]) {
        Runtime ru = Runtime.getRuntime();

        Process p = null;
        try {
            //I've added this as a bouns, this should not be neccessary(methinks)
            String[] s = {"C:\\Program Files\\ImageMagick-6.8.6-Q16"};
            String[] cmd = {"convert", "acc-logo.jpg","-flip", "edited.jpg"};
            p = ru.exec(cmd,s);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        BufferedReader ina = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line = null;
        try {
            while ((line = ina.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

最佳答案

可执行文件的路径中有空格,并且 Runtime.exec() 调用出现问题。使用 ProcessBuilder 代替;它更容易处理参数中的空格。

关于java - 使用 java 执行 imagemagick 命令没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008398/

相关文章:

java - 执行Linux命令的方法失败

php - apache webserver 已经在 centos xampp 中运行

c# - 从 Child UserControl 更改 MainWindow 中 TabControl 的 SelectedIndex

java - eclipse中跳过所有断点的快捷键

java - Hibernate:相对于自定义@Transactional(timeout)的默认事务超时

java - 使用数独解决方案检查器时遇到问题

java - 当我的数据完整时,为什么会收到 DataIntegrityViolationException?

r - 将脚本作为参数传递给 RGui

php - 从命令行运行 Zend Framework 操作

ios - 无法从命令行在模拟器上安装和运行 iOS 应用程序 - 小牛上的 iOS 6.1 Xcode 5 模拟器