#java #jvm #version #command-line-tool #macos #find #apple-silicon

app javalocate

Command line utility to find JVM versions on macOS, Linux and Windows

6 releases (3 breaking)

0.7.1 Dec 6, 2023
0.7.0 Nov 29, 2023
0.6.0 Apr 18, 2022
0.5.0 Mar 7, 2022

#174 in Operating systems

Download history 8/week @ 2024-08-02 23/week @ 2024-09-20 1/week @ 2024-09-27

51 downloads per month

Apache-2.0

38KB
625 lines

javalocate

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

Command line utility to find JVM versions on macOS, Linux (Debian, Ubuntu, RHEL/CentOS & Fedora) and Windows - useful for setting JAVA_HOME, 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:

brew tap dameikle/javalocate
brew install javalocate

Or using Cargo via the javalocate crate on crates.io:

cargo install javalocate

Usage

The utility is designed to be used in a similar fashion to the /usr/libexec/java_home 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)
    -r, --register-location <LOCATION>  Registers a custom JVM location directory to search in
    -x, --remove-location <LOCATION>    Removes a registered custom JVM location directory
    -l, --display-locations             Displays all the custom JVM location directories that are registered

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

javalocate -v 17

Or to get the path to the x86_64 JVM for Java 11

javalocate -v 11 -a x86_64

Or to get the path to latest aarch64 JVM available

javalocate -a aarch64

You can also specify a minimum version by appending a + to the version:

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 JAVA_HOME environment variable:

export JAVA_HOME=$(javalocate -v 11+ -f)

Default Locations

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
Windows Registry - HKEY_LOCAL_MACHINE\Software

It assumes that the release file is included in the JVM package on Linux and Windows, 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.

Custom Locations

You can add your own locations to search in using the Custom JVM Location options. This can be useful if you maintain your own manually installed JVM collections.

For example, if you manually install JVMs into the the /opt/jvms directory you can configure it to be searched using the --register-location (-r) command:

javalocate -r /opt/jvms
javalocate --register-location /opt/jvms

If you want to then remove that location, you can use the --remove-location (-x) command:

javalocate -x /opt/jvms
javalocate --remove-location /opt/jvms

You can list the currently registered location using the --display-locations (-l) command:

javalocate -l
javalocate --display-locations

Tips and Tricks

Bash Alias

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

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

Allows you to quickly flip between versions:

setjava 17
echo $JAVA_HOME
setjava 8
echo $JAVA_HOME
setjava 11
echo $JAVA_HOME

Powershell

You can set the version required in Powershell using the following syntax:

$env:JAVA_HOME=$(javalocate.exe -v 11)

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.

Dependencies

~10–22MB
~318K SLoC