← 返回 Skills 市场
longfer

Java Switch

作者 LongFer · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
254
总下载
0
收藏
1
当前安装
1
版本数
在 OpenClaw 中安装
/install java-switch
功能描述
在 macOS 上自动检测、安装并切换指定版本的 Java,配置 JAVA_HOME 和环境变量,支持持久化配置。
使用说明 (SKILL.md)

java-switch Skill

Description

在 macOS 上自动切换 Java 版本。使用 Homebrew 安装 OpenJDK,通过 /usr/libexec/java_home 管理多版本,自动配置环境变量。如果指定版本未安装,则自动安装并切换;如果已安装,则直接切换环境变量。

Trigger Phrases

  • "切换 Java 版本"
  • "切换 java 版本"
  • "安装并切换 Java"
  • "安装 java 版本"
  • "switch java version"
  • "change java version"
  • "install java"
  • "帮我切换到 java"
  • "帮我安装 java"
  • "配置 JAVA_HOME"

Parameters

  • version (required): Java 版本号,如 17, 11, 8, 21

Implementation

当用户请求切换 Java 版本时,按以下步骤执行:

步骤 1: 检查 Homebrew 是否已安装

if ! command -v brew &> /dev/null; then
    echo "Homebrew 未安装,正在安装..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

步骤 2: 检查指定版本的 Java 是否已安装

# 列出所有已安装的 Java 版本
/usr/libexec/java_home -V

# 检查特定版本是否已安装
/usr/libexec/java_home -v \x3Cversion> 2>/dev/null

步骤 3: 如果未安装,使用 Homebrew 安装

# 安装指定版本的 OpenJDK
brew install openjdk@\x3Cversion>

# Homebrew 安装后可能需要链接(根据版本提示)
# 通常路径:/opt/homebrew/opt/openjdk@\x3Cversion>/libexec/openjdk.jdk (Apple Silicon)
# 或:/usr/local/opt/openjdk@\x3Cversion>/libexec/openjdk.jdk (Intel)

步骤 4: 切换环境变量(当前会话)

# 使用 /usr/libexec/java_home 获取指定版本的 JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v \x3Cversion>)
export PATH=$JAVA_HOME/bin:$PATH

步骤 5: 持久化配置

# 添加到 ~/.zshrc
echo '' >> ~/.zshrc
echo '# Java \x3Cversion> configuration' >> ~/.zshrc
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v \x3Cversion>)' >> ~/.zshrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.zshrc

# 使配置生效
source ~/.zshrc

步骤 6: 验证

# 验证 Java 版本
java -version

# 验证 JAVA_HOME
echo $JAVA_HOME

Example Conversation

用户: 帮我切换到 Java 17

助手:

  1. 🔍 检查 Homebrew 是否已安装... ✅ 已安装
  2. 🔍 检查 Java 17 是否已安装... ⚠️ 未安装
  3. 📦 使用 Homebrew 安装 OpenJDK 17...
    brew install openjdk@17
    
  4. 🔄 配置环境变量...
    export JAVA_HOME=$(/usr/libexec/java_home -v 17)
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. 💾 持久化配置到 ~/.zshrc...
  6. ✅ 验证安装:
    openjdk version "17.0.2" 2021-10-19
    OpenJDK Runtime Environment (Homebrew) (build 17.0.2+8)
    
  7. ✅ 切换成功!JAVA_HOME: /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home

用户: 切换到 Java 11

助手:

  1. 🔍 检查 Java 11 是否已安装... ✅ 已安装
  2. 🔄 切换环境变量...
  3. ✅ 切换成功!当前 Java 版本:
    openjdk version "11.0.20" 2023-07-18 LTS
    

Notes

  • Homebrew 路径:
    • Apple Silicon (M1/M2): /opt/homebrew/opt/openjdk@\x3Cversion>/libexec/openjdk.jdk
    • Intel Mac: /usr/local/opt/openjdk@\x3Cversion>/libexec/openjdk.jdk
  • macOS 内置命令: /usr/libexec/java_home 用于动态获取 Java 路径,支持多版本管理
  • Shell 配置: macOS 10.15+ 默认使用 zsh,配置文件为 ~/.zshrc
  • 版本格式: 支持 8, 11, 17, 21 等主版本号,也支持完整版本号如 17.0.5
  • 如果用户没有指定版本号,询问用户需要哪个版本(推荐 17 或 21)

Related Scripts

  • scripts/switch-java.sh - 可复用的切换脚本

Troubleshooting

Homebrew 安装失败

  • 检查网络连接
  • 尝试使用国内镜像:export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"

Java 版本找不到

  • 运行 /usr/libexec/java_home -V 查看所有已安装版本
  • 确认版本号格式正确(如 17 而不是 java17

环境变量不生效

  • 确认已运行 source ~/.zshrc
  • 重启终端窗口
  • 检查 ~/.zshrc 中是否有冲突的配置
安全使用建议
This skill appears to do what it says: it may install Homebrew and an OpenJDK via brew, set JAVA_HOME, and append lines to ~/.zshrc (backing up the file first). Before running, consider: 1) review your current ~/.zshrc (and any existing startup code) because the script will source it which executes its contents; 2) the installer uses the official Homebrew install URL (raw.githubusercontent.com) and requires network access and possibly elevated privileges; 3) if you prefer manual control, run ./scripts/switch-java.sh <version> yourself rather than letting an agent execute it; and 4) keep the created backup (~/.zshrc.backup.TIMESTAMP) until you confirm the changes are correct.
功能分析
Type: OpenClaw Skill Name: java-switch Version: 1.0.0 The skill automates Java version management on macOS by installing Homebrew, downloading OpenJDK via brew, and updating ~/.zshrc. While it performs high-privilege actions such as executing the official Homebrew installation script via curl and modifying shell configuration files, these behaviors are explicitly documented and align with the stated purpose of the tool. No evidence of data exfiltration, obfuscation, or malicious intent was found in SKILL.md or scripts/switch-java.sh.
能力评估
Purpose & Capability
The skill declares and implements a macOS Java-version switcher. The included script and SKILL.md only perform Homebrew detection/installation, brew install openjdk@<version>, use /usr/libexec/java_home, update ~/.zshrc, and verify java -version — all coherent with the stated purpose.
Instruction Scope
Instructions modify and source the user's ~/.zshrc to persist JAVA_HOME/PATH. Sourcing ~/.zshrc will execute whatever commands are present there, so the agent running these steps will execute user shell startup code; this is expected for a tool that updates shell config but is worth calling out as a potential side-effect.
Install Mechanism
No arbitrary binary downloads are introduced by the skill. Homebrew is installed using the official installer script from raw.githubusercontent.com and OpenJDK is installed via brew — both are standard mechanisms for macOS package installation.
Credentials
The skill requests no environment variables, no credentials, and no config paths beyond the user's ~/.zshrc; this is proportionate to a Java version switcher.
Persistence & Privilege
The skill persists changes by appending entries to ~/.zshrc and creates a backup when replacing existing JAVA_HOME lines. It does not request always:true or system-wide configuration changes beyond the user's shell file.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install java-switch
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /java-switch 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release of java-switch skill. - Automates switching and installation of Java versions on macOS using Homebrew and `/usr/libexec/java_home`. - Supports trigger phrases in both Chinese and English for Java version switching and installation. - Automatically installs specified Java version if not present, or switches environment variables if already installed. - Updates environment variables for the current session and persists configuration to `~/.zshrc`. - Provides step-by-step user feedback and troubleshooting tips. - Supports major Java versions (e.g., 8, 11, 17, 21) and full version numbers.
元数据
Slug java-switch
版本 1.0.0
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 1
常见问题

Java Switch 是什么?

在 macOS 上自动检测、安装并切换指定版本的 Java,配置 JAVA_HOME 和环境变量,支持持久化配置。 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 254 次。

如何安装 Java Switch?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install java-switch」即可一键安装,无需额外配置。

Java Switch 是免费的吗?

是的,Java Switch 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Java Switch 支持哪些平台?

Java Switch 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Java Switch?

由 LongFer(@longfer)开发并维护,当前版本 v1.0.0。

💬 留言讨论