A lightweight command-line tool to switch between multiple installed JDKs using a JSON config file. This utility updates your JAVA_HOME, ensures the selected JDK is in your PATH, and persists the setup via your shell profile.
You’ll need:
gcc(typically comes with Xcode or Xcode Command Line Tools)cJSON(install via Homebrew:brew install cjson)
CJSON_DIR=$(brew --prefix cjson)
gcc -o build/jdk jdk.c -I"$CJSON_DIR/include" -L"$CJSON_DIR/lib" -lcjson
Create a config.json in the same directory as the binary:
{
"command": "/usr/libexec/java_home -V 2>&1 | sed -nE 's/.* (\/.*)$/\\1/p'",
"symlink_path": "~/.devtools/jdk",
"profile_file_path": "~/.zshrc",
"jdks": [
"~/Library/Java/JavaVirtualMachines/corretto-21.0.3/Contents/Home",
"~/Library/Java/JavaVirtualMachines/corretto-17.0.11/Contents/Home",
"~/Library/Java/JavaVirtualMachines/corretto-11.0.23/Contents/Home",
"~/Library/Java/JavaVirtualMachines/corretto-1.8.0_412/Contents/Home"
]
}
./jdk --list
./jdk --use 2This:
- Creates a symlink at
~/.devtools/jdkpointing to the selected JDK - Exports it as
JAVA_HOMEby updating the ~/.zshrc - Adds it to your
PATH(if not already) - Reloads your shell environment
- Only tested with
zsh, but should work with any shell that supports sourcing profile files andSIGUSR1trap. - If you modify
config.json, just re-run./jdk --use <index>to apply changes. - The
commandin config.json is for reference only, one use it to get current available JDKs on mac but still needs to populate the list manually in config.jsonjdkssection.