Questo articolo mostra come installare Java JDK su macOS, il gestore di pacchetti Homebrew, l’installazione manuale, e passare tra diverse versioni JDK.
Testato con
- macOS 11 Big Sur
- Homebrew 2.7.4
- JDK 8, 14, 16, 16 (AdoptOpenJDK e OpenJDK)
Topics
- Homebrew installa l’ultimo Java (OpenJDK) su macOS
- Homebrew installa Java 8 (OpenJDK) su macOS
- Homebrew installa un Java specificato (AdoptOpenJDK) su macOS
- Installa manualmente Java (Early-Access Builds) su macOS
- Passa tra diverse versioni di JDK
P.S Al momento della scrittura, l’ultima JDK GA è JDK 15, e la build ad accesso anticipato è JDK 16.
Nota
Da macOS 10.15 Catalina, la shell predefinita del terminale passa da bash (shell Bourne-again) a zsh (shell Z). E dovremmo spostare tutti gli script di avvio e le variabili d’ambiente in ~/.bash_profile
o ~/.bashrc
in ~/.zshenv
o ~/.zshrc
.
Inoltre, leggete questo file di avvio Zsh.
1. Homebrew installa l’ultimo Java su macOS
1.1 Installare e aggiornare Homebrew.
1.2 brew search java
per trovare tutta la formula Java disponibile.
% brew search java ==> Formulaeapp-engine-java java javacc jslint4java pdftk-javagoogle-java-format java11 javarepl libreadline-java
1.3 brew info
per mostrare i dettagli della formula.
La formula java
contiene sempre l’ultima versione GA di Java JDK (OpenJDK); al momento della scrittura, l’ultima GA è JDK 15.
% brew info javaopenjdk: stable 15.0.1 (bottled) Development kit for the Java programming languagehttps://openjdk.java.net//usr/local/Cellar/openjdk/15.0.1 (614 files, 324.9MB) Poured from bottle on 2020-12-09 at 09:06:07From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk.rbLicense: Cannot Represent
La formula java11
contiene la versione Java 11 LTS.
% brew info java11 openjdk@11: stable 11.0.9 (bottled) Development kit for the Java programming languagehttps://openjdk.java.net/Not installedFrom: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/[email protected]: GPL-2.0-only
1.4 brew install java
per installare l’ultima JDK 15.
% brew info java
1.5 Dove installa Homebrew il java?
Homebrew ha installato i file e le directory JDK a /usr/local/Cellar/openjdk/
, e il link simbolico a /usr/local/opt/openjdk
punta all’ultima versione Java 15.0.1
.
% ls -lsa /usr/local/Cellar/openjdk/ total 00 drwxr-xr-x 3 mkyong staff 96 Dec 9 09:06 .0 drwxrwxr-x 69 mkyong admin 2208 Jan 15 15:35 ..0 drwxr-xr-x 9 mkyong staff 288 Jan 15 16:47 15.0.1% ls -lsa /usr/local/opt/openjdk0 lrwxr-xr-x 1 mkyong admin 24 Dec 9 09:06 /usr/local/opt/openjdk -> ../Cellar/openjdk/15.0.1
1.6 La formula java
è keg-only, il che significa che è installata in /usr/local/Cellar
ma non collegata in posti come /usr/local/bin
o /Library/Java/JavaVirtualMachines/
(macOS /usr/bin/java
wrapper).
Per macOS /usr/bin/java
wrapper per trovare il JDK installato, creiamo manualmente un link simbolico a /Library/Java/JavaVirtualMachines/
.
% sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
1.7 Fatto.
% java -versionopenjdk version "15.0.1" 2020-10-20OpenJDK Runtime Environment (build 15.0.1+9)OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)
2. Homebrew installa Java 8 su macOS
2.1 Installare e aggiornare Homebrew.
2.2 Il brew search java
non ha java8
?
% brew search java ==> Formulaeapp-engine-java java javacc jslint4java pdftk-javagoogle-java-format java11 javarepl libreadline-java
2.3 Il Java 8 è disponibile alla formula openjdk@8
. Il openjdk
è uguale alla formula java
, e contiene sempre l’ultima versione JDK GA; il openjdk@11
è uguale alla formula java11
, contenente JDK 11.
% brew search openjdk==> Formulaeopenjdk openjdk@11 openjdk@8
2.4 Possiamo usare la formula openjdk@8
per installare Java 8 su macOS.
% brew install openjdk@8
2.5 Anche il openjdk@8
è un keg-only; dobbiamo creare un link simbolico in modo che il java
wrapper di macOS possa trovarlo.
% sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk
2.6 Fatto.
% java -versionopenjdk version "1.8.0_275"OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)
2.7 Cosa succede se abbiamo installato più versioni di Java?
ls -lsah /Library/Java/JavaVirtualMachines/ openjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdkopenjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk
Possiamo aggiornare il $PATH
a ~/.zshrc
, in modo che il macOS possa trovare il Java installato correttamente.
% java -versionopenjdk version "15.0.1" 2020-10-20OpenJDK Runtime Environment (build 15.0.1+9)OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)% echo 'export PATH="/usr/local/opt/openjdk@8/bin:$PATH"' >> ~/.zshrc % source ~/.zshrc% java -versionopenjdk version "1.8.0_275"OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)
$JAVA_HOME
In alternativa, possiamo impostare la variabile d’ambiente $JAVA_HOME
su macOS.
3. Homebrew installa un Java specificato (AdoptOpenJDK) su macOS.
Il rubinetto adoptopenjdk/openjdk contiene molte versioni diverse di JDK (AdoptOpenJDK).
3.1 Aggiungere un nuovo tap
(repository di terze parti) adoptopenjdk/openjdk
.
% brew tap adoptopenjdk/openjdk
3.2 Cerchiamo la formula adoptopenjdk
relativa.
% brew search adoptopenjdk==> Casksadoptopenjdk adoptopenjdk12-openj9 adoptopenjdk14-openj9-largeadoptopenjdk-jre adoptopenjdk12-openj9-jre adoptopenjdk15adoptopenjdk-openj9 adoptopenjdk12-openj9-jre-large adoptopenjdk15-jreadoptopenjdk-openj9-jre adoptopenjdk12-openj9-large adoptopenjdk15-openj9adoptopenjdk-openj9-jre-large adoptopenjdk13 adoptopenjdk15-openj9-jreadoptopenjdk-openj9-large adoptopenjdk13-jre adoptopenjdk15-openj9-jre-largeadoptopenjdk10 adoptopenjdk13-openj9 adoptopenjdk15-openj9-largeadoptopenjdk11 adoptopenjdk13-openj9-jre adoptopenjdk8adoptopenjdk11-jre adoptopenjdk13-openj9-jre-large adoptopenjdk8adoptopenjdk11-openj9 adoptopenjdk13-openj9-large adoptopenjdk8-jreadoptopenjdk11-openj9-jre adoptopenjdk14 adoptopenjdk8-openj9adoptopenjdk11-openj9-jre-large adoptopenjdk14-jre adoptopenjdk8-openj9-jreadoptopenjdk11-openj9-large adoptopenjdk14-openj9 adoptopenjdk8-openj9-jre-largeadoptopenjdk12 adoptopenjdk14-openj9-jre adoptopenjdk8-openj9-largeadoptopenjdk12-jre adoptopenjdk14-openj9-jre-large adoptopenjdk9
3.3 Scegliamo la formula adoptopenjdk14
per installare la JDK 14.
% brew install adoptopenjdk14installer: Package name is AdoptOpenJDKinstaller: Installing at base path /installer: The install was successful.package-id: net.adoptopenjdk.14.jdkversion: 14.0.2+12volume: /location: Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdkinstall-time: 1610720586adoptopenjdk14 was successfully installed!
3.4 La formula adoptopenjdk
crea la JDK home direttamente nella /Library/Java/JavaVirtualMachines/
.
% ls -lsa /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdkopenjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdkopenjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk
3.5 Poiché la home del JDK è già in /Library/Java/JavaVirtualMachines/
, non abbiamo bisogno di creare alcun link simbolico, basta esportare $PATH
e puntarlo al corretto adoptopenjdk-14.jdk
.
% echo 'export PATH="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home/bin:$PATH"' >> ~/.zshrc% source ~/.zshrc% java -versionopenjdk version "14.0.2" 2020-07-14OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)
Nota
Leggi anche questo Switch adoptopenjdk cask per usare il pkg #73145
4. Installare manualmente Java (Early-Access Builds) su macOS.
Per alcune ragioni, abbiamo bisogno di installare Java su macOS manualmente:
- Anti-Homebrew, gli sviluppatori amano controllare tutto.
- La build JDK non esiste nel repository Homebrew, come le build early-access, o Oracle JDK.
P.S Al momento della scrittura, la build JDK early-access è JDK 16.
4.1 Accedi al sito web della build JDK 16 early access e scarica il JDK.
4.2 Estrarre il file scaricato tar.gz
in /Library/Java/JavaVirtualMachines
% sudo tar xvzf ~/Downloads/openjdk-16-ea+32_osx-x64_bin.tar.gz -C /Library/Java/JavaVirtualMachines% ls -lsa /Library/Java/JavaVirtualMachinesadoptopenjdk-14.jdkjdk-16.jdkopenjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdkopenjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk
4.3 Aggiornate $PATH
e puntate al /Library/Java/JavaVirtualMachines/jdk-16.jdk
% echo 'export PATH="/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin:$PATH"' >> ~/.zshrc
4.4 Test JDK 16.
% source ~/.zshrc% java -versionopenjdk version "16-ea" 2021-03-16OpenJDK Runtime Environment (build 16-ea+32-2190)OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)
Fatto.
5. Passare tra diverse versioni di JDK
Dall’esempio 1 all’esempio 4, abbiamo installato quattro versioni di JDK su macOS.
5.1 Elencare tutte le versioni JDK su macOS.
% /usr/libexec/java_home -VMatching Java Virtual Machines (4): 16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home 15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home 14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home 1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home% ls -lsa /Library/Java/JavaVirtualMachinesadoptopenjdk-14.jdkjdk-16.jdkopenjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdkopenjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk
5.2 Aggiungere la seguente funzione nel ~/.zshrc
(per macOS 10..15 Catalina, e superiori) o ~/.bashrc
(prima di macOS 10.15 Catalina).
jdk() { version=$1 unset JAVA_HOME; export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version}
P.S Create il file ~/.zshrc
se non esiste.
5.3 Create il ~/.zshrc
per riflettere le modifiche.
% source ~/.zshrc
5.4 Passare da una versione JDK all’altra.
% java -versionopenjdk version "16-ea" 2021-03-16OpenJDK Runtime Environment (build 16-ea+32-2190)OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)% jdk 1.8openjdk version "1.8.0_275"OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)% jdk 14openjdk version "14.0.2" 2020-07-14OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)% jdk 15openjdk version "15.0.1" 2020-10-20OpenJDK Runtime Environment (build 15.0.1+9)OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)% jdk 16openjdk version "16-ea" 2021-03-16OpenJDK Runtime Environment (build 16-ea+32-2190)OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)
Altre letture
Come impostare $JAVA_HOME
variabile d’ambiente su macOS.
- Cosa dovrebbe/non dovrebbe andare in .zshenv, .zshrc, .zlogin, .zprofile, .zlogout?
- Wikipedia macOS
- Homebrew
- AdoptOpenJDK – TAP HomeBrew
- Come installare Maven su Mac OSX
- File di avvio di Zsh
mkyong
Fondatore di Mkyong.com, ama Java e le cose open source. Seguilo su Twitter. Se ti piacciono i miei tutorial, considera di fare una donazione a questi enti di beneficenza.