When trying to install OpenJDK 11 on Alpine Linux 3.9, you might encounter a frustrating situation where everything appears to be set up correctly, but running java -version
gives you:
/bin/sh: java: not found
This happens even though:
- The Java binary exists in the expected location
- PATH environment variable includes the correct directory
- which java points to the right executable
The issue stems from Alpine Linux using musl libc instead of glibc, which is what most Java distributions are built against. The error occurs because the Java binary can't find required libraries.
Here's a working solution that properly installs OpenJDK 11 on Alpine 3.9:
FROM alpine:3.9
# Install required packages
RUN apk add --no-cache \
curl \
tar \
libstdc++
# Set environment variables
ENV JAVA_HOME=/opt/openjdk-11
ENV PATH=$JAVA_HOME/bin:$PATH
# Download and install OpenJDK 11
RUN set -eux; \
export JAVA_VERSION=11.0.2; \
export JAVA_URL=https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz; \
export JAVA_SHA256=99be79935354f5c0df1ad293620ea36d13f48ec3ea870c838f20c504c9668b57; \
\
wget -O /openjdk.tgz "$JAVA_URL"; \
echo "$JAVA_SHA256 */openjdk.tgz" | sha256sum -c -; \
mkdir -p "$JAVA_HOME"; \
tar --extract --file /openjdk.tgz --directory "$JAVA_HOME" --strip-components 1; \
rm /openjdk.tgz; \
\
# Add missing library symlinks
ln -s /usr/lib/libz.so.1 /usr/lib/libz.so; \
ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
# Verify installation
RUN java -version
The solution includes several important fixes:
- Installing
libstdc++
which provides essential C++ runtime libraries - Creating symbolic links for missing library dependencies
- Properly setting environment variables
For a more maintainable solution, consider using AdoptOpenJDK's Alpine-compatible builds:
FROM alpine:3.9
RUN apk add --no-cache \
openjdk11-jre \
openjdk11-jdk
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk
ENV PATH=$PATH:$JAVA_HOME/bin
RUN java -version
If you still encounter issues, try these diagnostic steps:
# Check library dependencies
ldd $(which java)
# Verify file permissions
ls -la $JAVA_HOME/bin/java
# Test with absolute path
/opt/openjdk-11/bin/java -version
Remember that Alpine Linux's minimal nature means you often need to explicitly install dependencies that would be present by default in other distributions.
When attempting to set up OpenJDK 11 on an Alpine 3.9 container, everything appears correct during installation:
$ ls -lah $JAVA_HOME/bin/java
-rwxr-xr-x 1 668 668 8.5K Jan 18 05:20 /opt/openjdk-11/bin/java
$ which java
/opt/openjdk-11/bin/java
Yet when executing java -version
, the system returns:
/bin/sh: java: not found
The issue stems from Alpine Linux's use of musl libc instead of glibc that most JDK binaries are compiled against. The error manifests because:
- OpenJDK binaries expect glibc-specific symbols
- Alpine's minimal design excludes compatibility layers
- The binary appears present but fails at runtime
Option 1: Use Alpine-compatible JDK
The most reliable approach is using an Alpine-specific build:
# In Dockerfile
FROM alpine:3.9
RUN apk add --no-cache openjdk11-jdk
# Verify installation
RUN java -version
Option 2: Install glibc compatibility
For cases requiring the standard OpenJDK binary:
RUN apk add --no-cache libstdc++ curl \\
&& curl -Lso /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \\
&& curl -Lso glibc-2.29-r0.apk https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk \\
&& apk add glibc-2.29-r0.apk
After implementing either solution, confirm proper installation:
docker exec -it container_name sh -c "java -version; echo $JAVA_HOME"
When choosing between solutions:
Solution | Image Size | Startup Time | Compatibility |
---|---|---|---|
Alpine JDK | ~150MB | Fastest | Limited to Alpine |
glibc Layer | ~200MB | Slower | Broader compatibility |