如何在Ubuntu 16服务器上为所有用户安装兼容Tomcat的OpenJDK 9
Hey there, let's resolve this issue where the openjdk-9-internal build from apt is causing Tomcat exceptions. The internal build is an early pre-release with known compatibility bugs—here's how to get a stable, properly supported OpenJDK 9 that works with Tomcat and is accessible to all server users:
Step 1: Remove the problematic internal JDK
First, fully purge the existing broken installation to avoid conflicts:
sudo apt purge openjdk-9-jdk openjdk-9-jre sudo apt autoremove && sudo apt clean
Step 2: Install a stable OpenJDK 9 via SDKMAN
SDKMAN lets you install official, stable OpenJDK distributions (like Adoptium's builds) that are tested for compatibility. Here's how:
- Install SDKMAN (run this command directly):
curl -s "https://get.sdkman.io" | bash - Load SDKMAN into your current shell session:
source "$HOME/.sdkman/bin/sdkman-init.sh" - List available stable OpenJDK 9 builds:
Look for entries markedsdk list javastableunder the Java 9 section (e.g., something like9.0.7-adptfrom Adoptium). - Install your chosen stable version (replace
9.0.7-adptwith the actual version from the list):sdk install java 9.0.7-adpt - Set it as your current user's default JDK:
sdk default java 9.0.7-adpt
Step 3: Make the JDK accessible to all users
To ensure every server user can use this stable JDK, we'll link it to a system-wide path and update alternatives:
- Get the full path of your installed JDK:
You'll get a path likeecho $JAVA_HOME/home/your-username/.sdkman/candidates/java/9.0.7-adpt—copy this. - Create a system-wide symlink to this path:
sudo ln -s /home/your-username/.sdkman/candidates/java/9.0.7-adpt /usr/lib/jvm/openjdk-9-stable - Register the JDK with
update-alternatives:sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-9-stable/bin/java 100 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-9-stable/bin/javac 100 - Set the new JDK as the system default:
When prompted, enter the number corresponding tosudo update-alternatives --config java sudo update-alternatives --config javac/usr/lib/jvm/openjdk-9-stableto select it as default.
Step 4: Verify the setup
- Switch to another user (e.g.,
su - test-user) and run these commands to confirm the correct version is available:
You should see output like this (version numbers may vary slightly):java -version javac -versionopenjdk version "9.0.7"
OpenJDK Runtime Environment AdoptOpenJDK (build 9.0.7+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 9.0.7+10, mixed mode) - Start your Tomcat instance and verify the earlier exceptions no longer occur.
Why this works
The 9-internal build from Ubuntu's repos is an unfinished pre-release, missing critical fixes and compatibility patches required for Tomcat. By switching to a stable, officially supported OpenJDK 9 distribution, you eliminate those compatibility gaps.
内容的提问来源于stack exchange,提问作者Peter Lustig

