[英]Running "ProcessBuilder.start" on Docker fails
Install the libreoffice package in your Dcoker container, I'm trying to convert a JAVA application from Excel to a PDF file, but an error occurs. Install the libreoffice package in your Dcoker container, I'm trying to convert a JAVA application from Excel to a PDF file, but an error occurs. (The container is running on Google Cloud Run.)
(容器在 Google Cloud Run 上运行。)
The Dockerfile is below. Dockerfile 如下。
FROM maven:3.8-jdk-11
RUN apt-get -y update
RUN apt-get -y install libreoffice
RUN mvn package
FROM adoptopenjdk/openjdk11:alpine-jre
CMD ["java", "-jar", "/app.jar"]
The java code running in the container is below.在容器中运行的 java 代码如下。
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", "soffice --headless -convert-to pdf --outdir '/out' '/out/file.xlsx'");
Process process = builder.start();
The error message that is output is as follows. output的错误信息如下。
line 1: soffice:not found
After installing the package in the Dockerfile, when I executed the which command, "usr/bin/libreoffice" existed, so I think the path is correct.在Dockerfile中安装package后,当我执行which命令时,“usr/bin/libreoffice”存在,所以我认为路径是正确的。 (I may be wrong.)
(我可能错了。)
I think that there is a lack of information because I am a Docker beginner, I kindly thank you.我认为缺少信息,因为我是 Docker 初学者,谢谢。
The error says that /bin/sh
does not know where the binary soffice
is.错误说
/bin/sh
不知道二进制soffice
在哪里。 The "soffice" binary must be on the PATH for /bin/sh
. “soffice”二进制文件必须在
/bin/sh
的 PATH 上。 Launch a shell and check with which soffice
, you may be able to fix your shell profile to include the correct PATH directories for soffice before your code will work:启动 shell 并检查
which soffice
,您可能能够修复您的 shell 配置文件以包含 soffice 的正确 PATH 目录,然后您的代码才能工作:
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", "soffice --headless -convert-to pdf --outdir '/out' '/out/file.xlsx'");
But if you know the fully qualified path to the soffice binary you should be able to eliminate the use of sub-process shell and separate the command line arguments:但是,如果您知道 soffice 二进制文件的完全限定路径,您应该能够消除使用子进程 shell 并分离命令行 arguments:
String soffice = "/full/path/to/soffice"; // eg /usr/lib/libreoffice/program/soffice.bin
// Check with:
System.out.println(soffice +" exists: "+new File(soffice).exists());
ProcessBuilder builder = new ProcessBuilder(soffice, "--headless", "-convert-to pdf", "--outdir", "/out", "/out/file.xlsx");
If "soffice" is in the PATH provided to your JVM, then launch without sub-process shell and path ought to work.如果“soffice”在提供给您的 JVM 的 PATH 中,则在没有子进程 shell 的情况下启动并且路径应该可以工作。 You may be able to check
which soffice
before launching your Java application to see if this may work:您可以在启动 Java 应用程序之前检查
which soffice
,看看这是否可行:
String soffice = "soffice";
ProcessBuilder builder = new ProcessBuilder(soffice, "--headless", "-convert-to pdf", "--outdir", "/out", "/out/file.xlsx");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.