javalocate

license build Crates.io GitHub release (latest by date)

Command line utility to find JVM versions on macOS and Linux (Debian, Ubuntu, RHEL/CentOS & Fedora) - useful for setting JAVAHOME_, particularly on machines with different JVM versions and architectures.

I'm thinking of you, Java Devs with Apple Silicon hardware 🐱‍💻

Install

The utility can be installed using Homebrew via the homebrew-javalocate tap: bash brew tap dameikle/javalocate brew install javalocate Or using Cargo via the javalocate crate on crates.io: bash cargo install javalocate

Usage

The utility is designed to be used in a similar fashion to the /usr/libexec/javahome_ by providing a number of flags that can be passed to control the selection.

These are shown below:

OPTIONS: -a, --arch <ARCH> Architecture to filter on (e.g. x86_64, aarch64, amd64) -d, --detailed Print out full details -f, --fail Return error code if no JVM found -h, --help Print help information -n, --name <NAME> JVM Name to filter on -v, --version <VERSION> Version to filter on (e.g. 1.8, 11, 17, etc)

Outputs

By default, the utility outputs a single path location to the "top" JVM found, ordered by descending version (i.e. Java 17 > Java 8), prioritising the system architecture (i.e. aarch64 > x86_64 on a Apple Silicon Mac).

Passing the detailed flag (--detailed or -d) prints the full details of all JVMs found.

This flag can also be used in conjunction with filters to display full details for the filtered set.

Filtering

The filtering options of name, version and arch can be used in isolation or together to fine tune the selection.

For example, to get the path to Java 17 bash javalocate -v 17

Or to get the path to the x86_64 JVM for Java 11 bash javalocate -v 11 -a x86_64

Or to get the path to latest aarch64 JVM available bash javalocate -a aarch64

You can also specify a minimum version by appending a + to the version: bash javalocate -v 1.8+

Exit Code

By default, the utility returns an OK (0) exit code whether a JVM is found or not.

Setting the fail flag (--fail or _-f) changes this behaviour, returning a CONFIG ERROR (78) exit code.

This can be useful if you want to use the utility in a shell script.

For example, the below would return an error code if Java 11 or above could not be found when trying to set the JAVAHOME_ environment variable: bash export JAVA_HOME=$(javalocate -v 11+ -f)

Operating Systems Supported

The utility looks in the default JVM installation locations for the following operating systems:

| Operating System | Location | |------------------|-----------------------------------| | macOS | /Library/Java/JavaVirtualMachines | | Ubuntu | /usr/lib/jvm | | Debian | /usr/lib/jvm | | RHEL | /usr/lib/jvm | | CentOS | /usr/lib/jvm | | Fedora | /usr/lib/jvm |

It assumes that the release file is included in the JVM package on Linux, and the release file and Info.plist file is packaged on macOS.

Experimental support has been added to build information from path file name where release file is not available. This can occur on older JVMs.

Tips and Tricks

Bash Alias

Adding the following to your ~/.bashrc (or ~/.bashaliases_) file:

bash setjava() { export JAVA_HOME=`javalocate -v $1` }

Allows you to quickly flip between versions: bash setjava 17 echo $JAVA_HOME setjava 8 echo $JAVA_HOME setjava 11 echo $JAVA_HOME

Building

The utility is developed in Rust and can be build from source using:

cargo build

Or for a release version cargo build --profile release

Licence

Copyright 2022 David Meikle

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.