You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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-alternatives to manage them:
    1. Register each JDK version with:
      sudo update-alternatives --install /usr/bin/java java /path/to/your/jdk/bin/java 1
      
      Replace /path/to/your/jdk with the actual folder of your JDK, and adjust the priority number (higher = more preferred) as needed.
    2. Switch between versions anytime with:
      sudo update-alternatives --config java
      
      Just follow the prompt to pick the version you want to use globally.

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:

  1. First, find your JDK's installation path. If you installed via package manager, you can run update-alternatives --display java to get the full path (look for the "link currently points to" line). It'll look something like /usr/lib/jvm/java-17-openjdk-amd64.
  2. Edit your shell's configuration file:
    • If you use Bash: Open ~/.bashrc with nano ~/.bashrc
    • If you use Zsh: Open ~/.zshrc with nano ~/.zshrc
  3. 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
    
  4. Save and exit (in nano: press Ctrl+O, hit Enter, then Ctrl+X).
  5. 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

  1. Open Sublime Text, go to Tools > Build System > New Build System.
  2. 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"}
    }
    
  3. Save the file as Java.sublime-build (save it in the default "User" directory that pops up).
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:25:12