Linux命令无法在Java程序中正常运行

时间:2018-08-24 11:45:30

标签: java linux

我正在开发一个基于Spring的应用程序,在该应用程序中我们想使用ImageMagick进行某些图像处理。我们有一些命令,我​​想在文本处理之后尝试一下(主要是按特定长度分割字符串),然后调用ImageMagick命令。不幸的是,每当我运行Process类时,都会遇到奇怪的错误,但是当我在终端中复制粘贴确切的命令时,它会很好地工作。

示例代码:

 public @ResponseBody void setText(){
        String text = "Lorem ipsum <NAME> dolor sit amet, consectetuer adipiscing elit." +
                " Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                "et magnis dis <NAME> parturient montes, nascetur ridiculus mus. " +
                "Donec quam felis, ultricies nec, pellentesque eu, pretium quis";
        String processedText = "";
        if(text.length()>20){
            for (int i = 0; i < text.length(); i += 20) {
                //processedText += "\n";
                processedText += text.substring(i, Math.min(i + 20, text.length()));
            }
        }
        try {
            String path = "convert /home/akshay/output.png -font /home/akshay/cabin.ttf -gravity west -pointsize 13 " +
                    "-annotate +50+300 \n'"+processedText+"' /home/akshay/output.jpg";
            System.out.println("path is "+path);

            Process proc = Runtime.getRuntime().exec(path);

                BufferedReader stdInput = new BufferedReader(new
                        InputStreamReader(proc.getInputStream()));

                BufferedReader stdError = new BufferedReader(new
                        InputStreamReader(proc.getErrorStream()));

                System.out.println("Here is the standard output of the command:\n");
                String s = null;
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

错误日志:

path is convert /home/akshay/output.png -font /home/akshay/cabin.ttf -gravity west -pointsize 13 -annotate +50+300 
'Lorem ipsum <NAME> dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis <NAME> parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis' /home/akshay/output.jpg
Here is the standard output of the command:

Here is the standard error of the command (if any):

convert: unable to open image `ipsum': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `<NAME>': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `dolor': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `sit': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `amet,': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `consectetuer': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `adipiscing': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.

我们多次遇到此问题,标准命令通常很奇怪,但是我似乎找不到解决方案。

更新的命令

ProcessBuilder pb = new ProcessBuilder("convert", "/home/akshay/output.png", 
                "-gravity west","-pointsize 13","-annotate +50+300","'"+processedText+"'","/home/akshay/output.jpg");

错误日志:

Here is the standard output of the command:

Here is the standard error of the command (if any):

convert: unrecognized option `-gravity west' @ error/convert.c/ConvertImageCommand/1722.

尝试了第二个版本

   Process proc = Runtime.getRuntime().exec(new String[]{"convert", "/home/akshay/output.png", "-font /home/akshay/cabin.ttf",
                    "-gravity west","-pointsize 13","-annotate +50+300","'"+processedText+"'","/home/akshay/output.jpg"});

错误日志:

Here is the standard output of the command:

Here is the standard error of the command (if any):

convert: unrecognized option `-font /home/akshay/cabin.ttf' @ error/convert.c/ConvertImageCommand/1643.

2 个答案:

答案 0 :(得分:1)

带有数组的exec的要点是,每个命令,参数,值都在不同的String中。关键是您不必理会参数中的可能空间(如文本中的内容)。所以:

Process proc = new ProcessBuilder(
                "convert", 
                "test.png", 
                "-gravity", "west", 
                "-pointsize","13", 
                "-annotate", "+50+300", 
                processedText, 
                "test.png").start();

请注意,您无需添加任何'

你很好。

答案 1 :(得分:0)

传递给String[]方法的exec需要进行所有拆分。每个字符串都被视为命令的单个选项。因此,例如"-gravity west"作为一个整体传递给命令,该命令随后无法解释该命令。而是使用"-gravity", "west"拆分。因此,请在每个空间上分开。