Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux命令在Java程序中无法正常工作_Java_Linux - Fatal编程技术网

Linux命令在Java程序中无法正常工作

Linux命令在Java程序中无法正常工作,java,linux,Java,Linux,我正在开发一个基于Spring的应用程序,其中我们希望使用ImageMagick进行一些图像处理。我们有一些命令,我想在文本处理(主要是按特定长度拆分字符串)之后尝试,然后调用ImageMagick命令。不幸的是,每当我运行Process类时,都会出现奇怪的错误,但是当我在终端中复制粘贴确切的命令时,效果很好 示例代码: public @ResponseBody void setText(){ String text = "Lorem ipsum <NAME> do

我正在开发一个基于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();
        }
    }
错误日志:

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.
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.
尝试了第二种变体

   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 `-gravity west' @ error/convert.c/ConvertImageCommand/1722.
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.

传递给
exec
方法的
String[]
需要将所有内容拆分。每个字符串都被视为命令的单个选项。因此,例如,
“-gravity west”
作为一个整体传递给命令,该命令随后无法解释它。相反,使用
“-gravity”、“west”
拆分。因此,在每个空间上进行分割。

使用数组执行的要点是,每个命令、参数、值都位于不同的
字符串中,这就是。关键是,您不必担心参数中可能存在的空格(如在文本中)。因此:

请注意,您不需要添加任何


<> p>你很好。

为什么在命令行的中间<代码> \n>代码>?你的意图是什么?我会先尝试使用。不要试图自己创建完整路径。@Jean BaptisteYunès:在图像中换行,稍后我想根据图像乘以可用文本框的高度X宽度来计算我可以容纳的字符数。该\n稍后将被注释掉。感谢you@AxelH:我只有一个命令要运行。什么意思?你能详细说明一下吗。谢谢这个副本将表明,在命令和每个参数中使用数组而不是一个字符串更安全。这很有效。非常感谢。
Process proc = new ProcessBuilder(
                "convert", 
                "test.png", 
                "-gravity", "west", 
                "-pointsize","13", 
                "-annotate", "+50+300", 
                processedText, 
                "test.png").start();