JLink不生成可再分发的镜像。

huangapple go评论75阅读模式
英文:

JLink does not produce redistributable image

问题

有一段时间了,我一直在处理模块化项目,但由于受到文件名和自动模块的限制,我从未有机会使用jlink工具来生成可重新分发的应用程序镜像。今天,我决定开始一个独立的项目,不导入任何外部依赖,以防止使用兼容模式。该项目由3个模块组成,使用Maven构建,因此我将只发布我使用的jlink命令片段。

项目供参考:https://gitlab.com/Dragas/edu-day-demo,请查看modules-full标签。项目使用package目标构建,以防止污染您的本地.m2存储库。项目已配置为拉取依赖项,因此打包和部署将更加容易。

我用于生成jlinked镜像的命令如下:

jlink \
   --module-path edu-day-runtime/target/dependency/:edu-day-runtime/target/ \
   --add-modules ALL-MODULE-PATH \
   --output edu-day-jlinked \
   --launcher edurun=edu.day.runtime

执行此命令确实会生成一个jlinked镜像,其中包含了运行项目所需的最小模块、Java库和JVM二进制文件。在构建镜像的机器上运行以下命令

edu-day-jlinked/bin/edurun 1 1

将会运行项目并输出以下结果

Result of sum is 2

然而,在容器化环境中尝试运行相同的操作(这里我使用的是bash:5,一个非Java镜像,模拟了一个未安装Java的环境),结果不相似。相反,shell 似乎找不到名为java的二进制文件

docker run -it -v "$(pwd)/edu-day-jlinked:/app" bash:5
...(在容器中)
bash-5.0# /app/bin/edurun 1 1
/app/bin/edurun: 第4行: /app/bin/java: 未找到该文件或目录

经检查,该文件夹确实包含名为java的二进制文件

bash-5.0# ls -la
总用量 52
drwxr-xr-x    2 1000     1000          4096 8月 23 07:53 .
drwxr-xr-x    7 1000     1000          4096 8月 23 07:53 ..
-rwxr-xr-x    1 1000     1000           116 8月 23 07:53 edurun
-rwxr-xr-x    1 1000     1000         16688 8月 23 07:53 java
-rwxr-xr-x    1 1000     1000         16712 8月 23 07:53 keytool

但是,即使直接调用它(显示帮助消息),也不会产生任何结果,除了显示找不到二进制文件的相同消息

(在/app/bin/文件夹中)
bash-5.0# ./java 
bash: ./java: 没有那个文件或目录

更有趣的是,即使keytool二进制文件也返回相同的错误

(在/app/bin/文件夹中)
bash-5.0# ./keytool
bash: ./keytool: 没有那个文件或目录

这引发了一个问题:出了什么问题?我还没有深入研究jlink的工作原理,但我猜测它从我自己的Java安装(来自Arch存储库的openjdk 11.0.8+10)中复制了二进制文件,并认为这是可以重新分发的。或者我是不是错过了某些命令行选项?

英文:

For a while now I've been working with modular projects, but due to being constrained with filename and automatic modules, I had never got a chance to work with jlink tool to produce a redistributable application image. Today I've opted to start an independent project which does not import any external dependencies to prevent usage of the compatibility mode. The project consists of 3 modules and is in maven, so I will only be posting the jlink command snippet I'm using.

Project for reference: https://gitlab.com/Dragas/edu-day-demo, checkout the modules-full tag. Project is built with package goal, to prevent polluting your local .m2 repository. Project is already configured to pull dependencies so packaging and deployment would be easier.

The command I used to generate the jlinked image was as follows:

jlink \
   --module-path edu-day-runtime/target/dependency/:edu-day-runtime/target/ \
   --add-modules ALL-MODULE-PATH \
   --output edu-day-jlinked \
   --launcher edurun=edu.day.runtime

Invoking the command does indeed generate a jlinked image, which contains minimum required modules, java libraries and JVM binaries to run the project. Invoking the machine that built the image

edu-day-jlinked/bin/edurun 1 1

does run the project and outputs the following

Result of sum is 2

Meanwhile, attempting to run the same in containerized environment (here i'm using bash:5, a non-java image to simulate an environment where java is not installed) does not yield similar results. Instead, the shell does not seem to find a binary named java

docker run -it -v "$(pwd)/edu-day-jlinked:/app" bash:5
...(in container)
bash-5.0# /app/bin/edurun 1 1
/app/bin/edurun: line 4: /app/bin/java: not found

Upon inspection, the folder does indeed contain the binary called java

bash-5.0# ls -la
total 52
drwxr-xr-x    2 1000     1000          4096 Aug 23 07:53 .
drwxr-xr-x    7 1000     1000          4096 Aug 23 07:53 ..
-rwxr-xr-x    1 1000     1000           116 Aug 23 07:53 edurun
-rwxr-xr-x    1 1000     1000         16688 Aug 23 07:53 java
-rwxr-xr-x    1 1000     1000         16712 Aug 23 07:53 keytool

But even invoking it directly (to show the help message) does not yield any results, besides the same message that the binary cannot be found

(in /app/bin/ folder)
bash-5.0# ./java 
bash: ./java: No such file or directory

What is more interesting is that even the keytool binary returns the same error

(in /app/bin/ folder)
bash-5.0# ./keytool
bash: ./keytool: No such file or directory

This raises a question: what went wrong? I haven't yet delved deeper into how jlink works, but my speculation is that it copies the binaries from my own java installation (openjdk 11.0.8+10 from arch repositories), and considers that to be redistributable. Or did I just miss some command line options?

答案1

得分: 4

你的问题在于测试容器(bash:5)与 Java 环境不使用相同版本的运行时链接器。

由 jlink 生成的二进制文件只有在系统上存在兼容的 Linux 运行时链接器时才能运行。

运行时链接器的作用是在构建可执行文件时将二进制文件配置为在系统上执行。默认的运行时链接器在二进制文件中被硬编码。你可以使用诸如 readelf -lldd(仅当能找到运行时链接器时,ldd 才起作用)之类的工具来检查运行时链接器。

amd64 Linux(例如 Ubuntu)的默认运行时链接器是:/lib64/ld-linux-x86-64.so.2

i386 Linux 的默认运行时链接器是:/lib/ld-linux.so.2

bash:5 容器中,默认的运行时链接器是:/lib/ld-musl-x86_64.so.1

这与 JDK 的运行时链接器不兼容。

错误信息 /app/bin/java: not found 是由于无法找到二进制文件的运行时链接器。在 bash:5 容器中进行 jlinked VM 的简单测试也会出现相同的错误。

当我获取了所使用 Java 的运行时链接器时:

$ docker run --rm -it -v (pwd)/edu-day-jlinked64:/app -w /here bash:5 bash

bash-5.0# /app/bin/java
bash: /app/bin/java: No such file or directory
bash-5.0# strings -a /app/bin/java | grep '^/lib'
/lib64/ld-linux-x86-64.so.2
bash-5.0# ls -l /lib64/ld-linux-x86-64.so.2
ls: /lib64/ld-linux-x86-64.so.2: No such file or directory

使用内置的运行时链接器进行测试:

bash-5.0# /lib/ld-musl-x86_64.so.1 --list /app/bin/java
/lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libjli.so => /app/bin/../lib/libjli.so (0x7fe28528c000)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libz.so.1 => /lib/libz.so.1 (0x7fe285272000)
libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
Error relocating /app/bin/../lib/libjli.so: __snprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __vfprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __read_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __memmove_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __printf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __fprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __sprintf_chk: symbol not found

所以在这里肯定不会起作用。

让我们使用一些“标准”的东西。由于我在 ubuntu:focal 容器中构建了 jlinked 应用,并安装了 Java 版本,让我们使用一个没有内置 Java 的版本:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked64:/app -w /here ubuntu:focal bash
root@865c9c12c029:/here# /app/bin/java
Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (to execute the main class in a module)
   or  java [options] <sourcefile> [args]
           (to execute a single source-file program)

这将在这种情况下起作用。

可复现性:

使用以下方式构建:

$ docker run --rm -it -v $(pwd):/here -w /here ubuntu:focal bash

# apt-get update
# DEBIAN_FRONTEND=noninteractive apt-get install -y git openjdk-14-jdk maven
# git clone https://gitlab.com/Dragas/edu-day-demo .
# git checkout modules-full
# ./mvnw package
# rm -rf edu-day-runtime/target/classes
# jlink --module-path edu-day-runtime/target/dependency/:edu-day-runtime/target/ --add-modules ALL-MODULE-PATH --output edu-day-jlinked    --launcher edurun=edu.day.runtime
# ./edu-day-jlinked/bin/edurun 1 1
Result of sum is 2

在相邻的目录中:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked:/app -w /here bash:5 bash
bash-5.0# /app/bin/edurun 1 1
/app/bin/edurun: line 4: /app/bin/java: not found

在另一个目录中:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked:/app -w /here ubuntu:focal bash
root@200b4a98f9ee:/here# /app/bin/edurun 1 1
Result of sum is 2
英文:

Your issue is that the test container (bash:5) doesn't use the same version of the run-time linker as the java environment.

The binary produced by the jlink will only run if there is a compatible linux run-time linker on the system.

The purpose of the run-time linker is to configure the binary for execution on the system - at the time you are building an executable the default run-time linker is hard-coded into the binary. You can inspect the run-time linker using a tool such as readelf -l, or ldd (ldd only works if it can find the run-time linker)

The default run-time linker for amd64 linux (e.g. ubuntu) is: /lib64/ld-linux-x86-64.so.2

The default run-time linker for i386 linux is: /lib/ld-linux.so.2

On a bash:5 container, the default run-time linker is: /lib/ld-musl-x86_64.so.1

This is not compatible with the run-time linker for the jdk

The error: /app/bin/java: not found is caused because the run-time linker cannot be found for the binary. A dirty test of a jlinked VM in a bash:5 container gives the same error.

When I get the run-time linker for the java I've used:

$ docker run --rm -it -v (pwd)/edu-day-jlinked64:/app -w /here bash:5 bash

bash-5.0# /app/bin/java
bash: /app/bin/java: No such file or directory
bash-5.0# strings -a /app/bin/java | grep &#39;^/lib&#39;
/lib64/ld-linux-x86-64.so.2
bash-5.0# ls -l /lib64/ld-linux-x86-64.so.2
ls: /lib64/ld-linux-x86-64.so.2: No such file or directory

Testing with the run-time linker that's on-board:

bash-5.0# /lib/ld-musl-x86_64.so.1 --list /app/bin/java
/lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libjli.so =&gt; /app/bin/../lib/libjli.so (0x7fe28528c000)
libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libz.so.1 =&gt; /lib/libz.so.1 (0x7fe285272000)
libdl.so.2 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
libpthread.so.0 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7fe2852a3000)
Error relocating /app/bin/../lib/libjli.so: __snprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __vfprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __read_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __memmove_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __printf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __fprintf_chk: symbol not found
Error relocating /app/bin/../lib/libjli.so: __sprintf_chk: symbol not found

so it definitely won't work here.

Let's use something 'standard'. As I had built the jlinked app in an ubuntu:focal container, with an installed version of java let's use one that doesn't have java built-in:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked64:/app -w /here ubuntu:focal bash
root@865c9c12c029:/here# /app/bin/java
Usage: java [options] &lt;mainclass&gt; [args...]
           (to execute a class)
   or  java [options] -jar &lt;jarfile&gt; [args...]
           (to execute a jar file)
   or  java [options] -m &lt;module&gt;[/&lt;mainclass&gt;] [args...]
       java [options] --module &lt;module&gt;[/&lt;mainclass&gt;] [args...]
           (to execute the main class in a module)
   or  java [options] &lt;sourcefile&gt; [args]
           (to execute a single source-file program)

so it will work in this case.

Reproducibility:

Built using:

$ docker run --rm -it -v $(pwd):/here -w /here ubuntu:focal bash

# apt-get update
# DEBIAN_FRONTEND=noninteractive apt-get install -y git openjdk-14-jdk maven
# git clone https://gitlab.com/Dragas/edu-day-demo .
# git checkout modules-full
# ./mvnw package
# rm -rf edu-day-runtime/target/classes
# jlink --module-path edu-day-runtime/target/dependency/:edu-day-runtime/target/ --add-modules ALL-MODULE-PATH --output edu-day-jlinked    --launcher edurun=edu.day.runtime
# ./edu-day-jlinked/bin/edurun 1 1
Result of sum is 2

In an adjacent directory:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked:/app -w /here bash:5 bash
bash-5.0# /app/bin/edurun 1 1
/app/bin/edurun: line 4: /app/bin/java: not found

In another directory:

$ docker run --rm -it -v $(pwd)/edu-day-jlinked:/app -w /here ubuntu:focal bash
root@200b4a98f9ee:/here# /app/bin/edurun 1 1
Result of sum is 2

答案2

得分: 3

TL;DR — The bash:5 image uses a C library that is binary incompatible with the C library that was linked with your edu-day-jlinked/bin/java executable.


The long version

> „…This raises a question: what went wrong?…

What's going wrong is your app/bin/java binary is failing to find the C library that it was originally linked to when you built your edu-day-jlinked executable on whatever machine you built it on locally.

The problem occurs because the java binary that jlink produced is linked to the GNU glibc library that your locally-installed JDK uses…

$ ldd edu-day-demo-modules-full/edu-day-jlinked/bin/java
        …
        libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa61a95b000)
        …

Whereas, the bash:5 image runs in the Busybox Linux distribution. And Busybox does not use glibc

bash-5.0# ldd app/bin/java
        …
        libjli.so =&gt; app/bin/../lib/jli/libjli.so (0x7f572a16d000)
        …
        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f572a19f000)
Error relocating app/bin/../lib/jli/libjli.so: __snprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __vfprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __read_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __memmove_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __printf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __fprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __sprintf_chk: symbol not found

It uses a different C library: musl

bash-5.0# find / -name &#39;*musl*&#39;
/lib/libc.musl-x86_64.so.1
/lib/ld-musl-x86_64.so.1

It helps to understand the Linking process. And it also helps to bear in mind that JLink builds a custom executable for a specific environment.

The proposed solution

> „…docker here is intended to simulate an environment which does not have java installed…

Here is a Dockerfile that successfully builds your application and the resulting image „does not have java installed“…

FROM maven:3.6.1-jdk-13-alpine as build

WORKDIR /app

COPY pom.xml .
COPY edu-day-sum edu-day-sum
COPY edu-day-runtime edu-day-runtime
COPY edu-day-api edu-day-api

RUN mvn package &amp;&amp; \
   --module-path ${JAVA_HOME}/jmods:edu-day-runtime/target/dependency/:edu-day-runtime/target/edu-day-runtime-1.0-SNAPSHOT.jar \
   --add-modules ALL-MODULE-PATH \
   --output edu-day-jlinked \
   --launcher edurun=edu.day.runtime

FROM alpine:latest

COPY --from=build /app/edu-day-jlinked /app

ENTRYPOINT [&quot;/app/bin/edurun&quot;]
CMD [&quot;1&quot;, &quot;1&quot;]

Docker best practice advises: „Use multi-stage builds“ (like in the above Dockerfile) when your aim is to build „an environment which does not have java installed“.

The FROM maven:3.6.1-jdk-13-alpine stage of the multi-stage build, uses an alpine Linux image that has both Maven and a JDK specifically built to be compatible with the alpine distribution.

The FROM alpine:latest is a very small linux distro that does not have Java on it. The maven:3.6.1-jdk-13-alpine layer is discarded as the Docker best practice docs says. The only java in the resulting image is the one in app/bin.

英文:

TL;DR — The bash:5 image uses a C library that is binary incompatible with the C library that was linked with your edu-day-jlinked/bin/java executable.


The long version

> „…This raises a question: what went wrong?…

What's going wrong is your app/bin/java binary is failing to find the C library that it was originally linked to when you built your edu-day-jlinked executable on whatever machine you built it on locally.

The problem occurs because the java binary that jlink produced is linked to the GNU glibc library that your locally-installed JDK uses…

$ ldd edu-day-demo-modules-full/edu-day-jlinked/bin/java
        …
        libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa61a95b000)
        …

Whereas, the bash:5 image runs in the Busybox Linux distribution. And Busybox does not use glibc

bash-5.0# ldd app/bin/java
        …
        libjli.so =&gt; app/bin/../lib/jli/libjli.so (0x7f572a16d000)
        …
        libc.so.6 =&gt; /lib64/ld-linux-x86-64.so.2 (0x7f572a19f000)
Error relocating app/bin/../lib/jli/libjli.so: __snprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __vfprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __read_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __memmove_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __printf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __fprintf_chk: symbol not found
Error relocating app/bin/../lib/jli/libjli.so: __sprintf_chk: symbol not found

It uses a different C library: musl

bash-5.0# find / -name &#39;*musl*&#39;
/lib/libc.musl-x86_64.so.1
/lib/ld-musl-x86_64.so.1

It helps to understand the Linking process. And it also helps to bear in mind that JLink builds a custom executable for a specific environment.

Your trial run on your local machine worked, because jlink built the executable specifically for your local environment.

The proposed solution

> „…docker here is intended to simulate an environment which does not have java installed…

Here is a Dockerfile that successfully builds your application and the resulting image „does not have java installed“…

FROM maven:3.6.1-jdk-13-alpine as build

WORKDIR /app

COPY pom.xml .
COPY edu-day-sum edu-day-sum
COPY edu-day-runtime edu-day-runtime
COPY edu-day-api edu-day-api

RUN mvn package &amp;&amp; \
   --module-path ${JAVA_HOME}/jmods:edu-day-runtime/target/dependency/:edu-day-runtime/target/edu-day-runtime-1.0-SNAPSHOT.jar \
   --add-modules ALL-MODULE-PATH \
   --output edu-day-jlinked \
   --launcher edurun=edu.day.runtime

FROM alpine:latest

COPY --from=build /app/edu-day-jlinked /app

ENTRYPOINT [&quot;/app/bin/edurun&quot;]
CMD [&quot;1&quot;, &quot;1&quot;]

Docker best practice advises: „Use multi-stage builds“ (like in the above Dockerfile) when your aim is to build „an environment which does not have java installed“.

The FROM maven:3.6.1-jdk-13-alpine stage of the multi-stage build, uses an alpine Linux image that has both Maven and a JDK specifically built to be compatible with the alpine distribution.

The FROM alpine:latest is a very small linux distro that does not have Java on it. The maven:3.6.1-jdk-13-alpine layer is discarded as the Docker best practice docs says. The only java in the resulting image is the one in app/bin.

huangapple
  • 本文由 发表于 2020年8月23日 16:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63544874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定