如何在Debian中优雅升级javac至8版本?附当前版本信息
Hey there! It looks like you've got a common mismatch where your Java runtime (JRE) is already on 1.8, but your compiler (javac) is stuck on 1.7. Let's fix this with Debian's built-in tools for a clean, maintainable solution.
First, let's confirm what JDK packages are installed on your system. Run this command to list openjdk-related packages:
dpkg --list | grep openjdk
You'll probably see openjdk-7-jdk (which provides your current javac 1.7) and openjdk-8-jre (which gives your Java 1.8 runtime).
Step 1: Install OpenJDK 8 JDK
If openjdk-8-jdk isn't already in the list from the previous command, install it using apt:
sudo apt update && sudo apt install openjdk-8-jdk
This will install the full JDK 8 package, including the javac 1.8 compiler.
Step 2: Set javac 1.8 as the Default
Debian uses update-alternatives to manage default system tools—this is the most elegant way to switch between versions without messing with paths manually.
Run this command to configure the default javac:
sudo update-alternatives --config javac
You'll see output similar to this (the exact numbers may vary):
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
- 0 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 auto mode
1 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1061 manual modePress
to keep the current choice[*], or type selection number:
Type the number next to /usr/lib/jvm/java-8-openjdk-amd64/bin/javac and hit enter.
Step 3: Verify the Fix
Check your javac version now—you should see 1.8.x:
javac -version
And confirm your Java runtime is still on 1.8 (it should be, but it's good to double-check):
java -version
Optional: Set JAVA_HOME (If Needed)
If your applications rely on the JAVA_HOME environment variable, you can set it to point to JDK 8. Edit your ~/.bashrc file:
nano ~/.bashrc
Add this line at the end (adjust the path if your JDK 8 is installed in a different location):
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
Save and exit, then reload the bashrc:
source ~/.bashrc
That's it! This approach keeps your system clean, uses Debian's native package management, and makes it easy to switch back to Java 7 later if you need to—just run sudo update-alternatives --config javac again and select the 1.7 option.
内容的提问来源于stack exchange,提问作者YAV

