Home > Java > javaTutorial > How Do I Set the Default Java Version on macOS?

How Do I Set the Default Java Version on macOS?

Linda Hamilton
Release: 2024-12-15 11:35:10
Original
648 people have browsed it

How Do I Set the Default Java Version on macOS?

How to Configure the Default Java Version in macOS

Introduction

This article provides a comprehensive overview of how to modify the default Java version on a macOS system.

Solution

The first step is to determine the available Java versions:

/usr/libexec/java_home -V
Copy after login

This command outputs a list of versions and their corresponding paths.

To set a specific version as the default, assign the path to the JAVA_HOME environment variable. For example, to set version 1.6.0_65-b14-462:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
Copy after login

You can also specify the major Java version:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Copy after login

Verifying the change:

java -version
Copy after login

The output should display the correct Java version.

To make the change permanent, add the export JAVA_HOME… command to your shell's initialization file:

Bash:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
Copy after login

Fish:

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Copy after login

Zsh:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Copy after login

Once the change is made, you can source the initialization file to activate it:

source ~/.zshrc
Copy after login

The above is the detailed content of How Do I Set the Default Java Version on macOS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template