如何在Linux中像Windows一样设置路径变量并全局配置JDK?
Hey there! Let's work through your Java setup questions on Linux—this is a super common pain point when you're getting multiple dev tools up and running, so you're definitely not alone here.
1. Ditch Multiple JDK Copies: Use a System-Wide Installation
First off, you don't need to copy JDK into every app's directory! That's just wasting disk space and making it a nightmare to update later. The better approach is to set up a system-wide JDK that all your tools can share (or manage multiple versions cleanly if you need them).
- For Debian/Ubuntu-based distros: Install via your package manager with a command like:
sudo apt install openjdk-17-jdk # Swap 17 for 11, 8, etc., depending on your needs - For RHEL/CentOS/Fedora: Use dnf instead:
sudo dnf install java-17-openjdk-devel - If you need to switch between multiple JDK versions (e.g., some apps need Java 8, others 17), use
update-alternativesto manage them:- Register each JDK version with:
Replacesudo update-alternatives --install /usr/bin/java java /path/to/your/jdk/bin/java 1/path/to/your/jdkwith the actual folder of your JDK, and adjust the priority number (higher = more preferred) as needed. - Switch between versions anytime with:
Just follow the prompt to pick the version you want to use globally.sudo update-alternatives --config java
- Register each JDK version with:
2. Make java Work Everywhere in Terminal (Like Windows)
This all boils down to setting up environment variables correctly—here's how to do it permanently:
- First, find your JDK's installation path. If you installed via package manager, you can run
update-alternatives --display javato get the full path (look for the "link currently points to" line). It'll look something like/usr/lib/jvm/java-17-openjdk-amd64. - Edit your shell's configuration file:
- If you use Bash: Open
~/.bashrcwithnano ~/.bashrc - If you use Zsh: Open
~/.zshrcwithnano ~/.zshrc
- If you use Bash: Open
- Add these lines to the end of the file (replace the JAVA_HOME path with your actual one):
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/bin - Save and exit (in nano: press
Ctrl+O, hit Enter, thenCtrl+X). - Make the changes take effect immediately with:
source ~/.bashrc # Or source ~/.zshrc if you're using Zsh
Now you can open any terminal window, type java -version or javac -version, and it'll work without switching directories—just like on Windows!
3. Configure JDK for Sublime Text
Sublime Text needs to know where your JDK is to compile and run Java code. You have two straightforward options:
Option 1: Custom Build System with Explicit JDK Path
- Open Sublime Text, go to
Tools > Build System > New Build System. - Replace the default text with this configuration (update the JAVA_HOME path to match your setup):
{ "cmd": ["$JAVA_HOME/bin/javac", "$file_name", "&&", "$JAVA_HOME/bin/java", "$file_base_name"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "shell": true, "env": {"JAVA_HOME": "/usr/lib/jvm/java-17-openjdk-amd64"} } - Save the file as
Java.sublime-build(save it in the default "User" directory that pops up). - Open your Java file, press
Ctrl+B, and Sublime will compile and run it using your specified JDK.
Option 2: Use Your Global Environment Variables
If you already set up the system-wide JAVA_HOME and PATH as we did earlier, you can simplify the build system to use the global javac and java commands directly:
{ "cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"], "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "selector": "source.java", "shell": true }
This way, Sublime will automatically use whichever JDK version you have set globally—no need to update the build system if you switch versions later.
内容的提问来源于stack exchange,提问作者nikk

