【问题标题】:The problem with Java Runtime Exec on Docker ContainerDocker Container 上的 Java Runtime Exec 的问题
【发布时间】:2023-03-28 01:28:01
【问题描述】:

我正在尝试使用 GDAL 包(用于 ogr2ogr 命令)对 Java 应用程序进行 dockerize。

我的 Dockerfile 是:

FROM openjdk:10
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

RUN apt-get update
RUN apt-get -y install python3-gdal
RUN apt-get -y install libgdal28
RUN apt-get -y install libgdal-perl-doc
RUN apt-get -y install libgdal-perl
RUN apt-get -y install libgdal-dev
RUN apt-get -y install gdal-data
RUN apt-get -y install gdal-bin

CMD ["java", "-jar", "/app.jar"]

在容器上运行的Java代码中有一个sn-p:

String command = "ogrinfo PG:\"host=host.docker.internal user=postgres dbname=test password=postgres\"";
Process process = Runtime.getRuntime().exec(command);

那么,输出是:

Unable to open datasource `PG:"host=host.docker.internal' with the following drivers.

但是,当我尝试直接从 bash 对容器运行命令时,它变得成功:

root@7b5fb10431cf:/# ogrinfo PG:"host=host.docker.internal user=postgres dbname=test password=postgres"
INFO: Open of `PG:host=host.docker.internal user=postgres dbname=test password=postgres'
      using driver `PostgreSQL' successful.

为什么会存在这样的差异?

【问题讨论】:

    标签: java docker gdal ogr2ogr


    【解决方案1】:

    连接String的语法很重要PostgreSQL / PostGIS,可能问题出在Process实例Java Runtime.exec上。 PG 命令必须以与您直接在容器中相同的方式运行,这应该适合您:

    String connection = "\"host=host.docker.internal user=postgres dbname=test password=postgres\"" ;
    String[] commands = {"bash","ogrinfo","PG:"+connection};
    Process process = Runtime.getRuntime().exec(commands);
    

    【讨论】:

    • 很遗憾,它没有执行“bash”命令。没有“bash”命令,我收到了相同的响应。
    【解决方案2】:

    根据@Hajed.Kh 的回答,我尝试了这个并且成功了:

        String[] commands = {
                "/bin/sh",
                "-c",
                command
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多