gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:54 25 4
gpt4 key购买 nike

我正在开发一个基于 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.

最佳答案

带有数组的 exec 的要点是每个命令、参数、值都在不同的 String 中,这就是。关键是您不必担心参数中可能的空间(就像在您的文本中)。所以:

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

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

你就可以开始了。

关于java - Linux 命令在 Java 程序中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com