← Back to Skills Marketplace
254
Downloads
0
Stars
1
Active Installs
1
Versions
Install in OpenClaw
/install java-switch
Description
在 macOS 上自动检测、安装并切换指定版本的 Java,配置 JAVA_HOME 和环境变量,支持持久化配置。
README (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
助手:
- 🔍 检查 Homebrew 是否已安装... ✅ 已安装
- 🔍 检查 Java 17 是否已安装... ⚠️ 未安装
- 📦 使用 Homebrew 安装 OpenJDK 17...
brew install openjdk@17 - 🔄 配置环境变量...
export JAVA_HOME=$(/usr/libexec/java_home -v 17) export PATH=$JAVA_HOME/bin:$PATH - 💾 持久化配置到 ~/.zshrc...
- ✅ 验证安装:
openjdk version "17.0.2" 2021-10-19 OpenJDK Runtime Environment (Homebrew) (build 17.0.2+8) - ✅ 切换成功!JAVA_HOME:
/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home
用户: 切换到 Java 11
助手:
- 🔍 检查 Java 11 是否已安装... ✅ 已安装
- 🔄 切换环境变量...
- ✅ 切换成功!当前 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
- Apple Silicon (M1/M2):
- 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中是否有冲突的配置
Usage Guidance
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.
Capability Analysis
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.
Capability Assessment
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.
How to Use
- Make sure OpenClaw is installed (local or Docker)
- Run the install command in chat:
/install java-switch - After installation, invoke the skill by name or use
/java-switch - Provide required inputs per the skill's parameter spec and get structured output
Version History
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.
Metadata
Frequently Asked Questions
What is Java Switch?
在 macOS 上自动检测、安装并切换指定版本的 Java,配置 JAVA_HOME 和环境变量,支持持久化配置。 It is an AI Agent Skill for Claude Code / OpenClaw, with 254 downloads so far.
How do I install Java Switch?
Run "/install java-switch" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.
Is Java Switch free?
Yes, Java Switch is completely free, licensed under MIT-0. You can download, install and use it at no cost.
Which platforms does Java Switch support?
Java Switch is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).
Who created Java Switch?
It is built and maintained by LongFer (@longfer); the current version is v1.0.0.
More Skills