Try   HackMD

在macOS Mojave 10.14上安裝AdoptOpenJDK及設定JAVA_HOME環境變數

tags: JDK macOS Mojave

寫在前頭

Hi各位瀏覽此筆記的訪客們大家好,這是我在HackMD上發表的首篇作品,如有錯誤資訊、技術知識不足之處,尚請包容!

關於我的Mac | About My Mac

如圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2012年中的老Macbook Pro

  • CPU:i5-3210M
  • RAM:16GB
  • SSD:WD Blue
    RAM及SSD是前幾年自己拆機升級的,因而得以大致流暢使用至今

什麼是JDK | What is JDK?

JDK

JDK是 Java Development Kit 的縮寫,包含了一系列開發 Java 時會使用的工具,包含了編譯器(Compiler)、執行工具等等⋯⋯ JDK 也包含了 JRE (Java Runtime Environment),即 Java 執行環境。

JRE&JVM

JRE包含了 JVM (Java Virtual Machine),因為 Java 程式必須透過 JVM 執行,才能轉換為目標作業系統可以執行的語言,所以 JRE 是不可或缺的。
一般電腦會預裝 JRE ,但不一定會預裝 JDK 。

為什麼要安裝JDK? | Why should I install JDK?

系統預裝的版本可能過舊、不符合開發要求、想要使用其他版本的JDK,又或者你單純就是不裝最新版就不開心(?
Oracle官方提供的查詢方法如下
打開終端機(Terminal)後輸入 java -version
可以看到,以 macOS Mojave 10.14 來說,預裝的JDK版本是 1.8.0_181 ,如圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Java SE? OpenJDK?

Java SE

Java SE的全稱為 Java Platform , Standard Edition ,除了 Java SE 另有 Java EE (Enterprise Edition) 及 ME (Micro Edition)。
Java SE Development Kit 可以直接在 Oracle 官網下載,需要同意相關條款。

OpenJDK

關於 OpenJDK ,一句話

OpenJDK是Java SE的自由、開源、免費的實作版本
Wikipedia

AdoptOpenJDK

說了這麼多,這次要安裝的是 AdoptOpenJDK ,由社群維護,並且開源。
首先進入網站,網頁會自動偵測目前使用的作業系統,版本選擇 OpenJDK 11(LTS) ,右側 JVM 維持 Hotspot ,點擊下方按鈕開始下載。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

下載的檔案是一個壓縮檔,如圖,對其雙擊就可以解壓縮
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

BTW,在此介紹一個好用的解壓縮軟體:The Unarchiver
解壓縮後會得到這樣的檔案
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

下一步,我們要把這個檔案移動到 macOS 下存放 JDK 的目錄。打開 Finder ,同時按下 Shift+⌘+G ,並在視窗中輸入 /Library/Java/JavaVirtualMachines
將剛剛解壓縮的檔案移動到這個目錄下,此操作會需要輸入使用者密碼以獲得更高權限。
完成上述操作後,接著再次在終端機Terminal中輸入 java -version以確認是否生效,預設會回傳最高版本,因此執行結果應該如下圖
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可以看到,顯示的版本已經是我們所下載的 OpenJDK 11

設定JAVA_HOME環境變數

為什麼設定環境變數?

設定環境變數的目的在於,讓系統可以直接調用 JDK 所在的目錄,才不用每次都輸入。因此許多需要使用到 JDK 的程式如 Eclipse ,僅需向系統調用 JAVA_HOME ,系統便會自動指向預先設定好的位置。

那麼開始吧!回到終端機 Terminal ,首先新增一個用於設定環境變數的文件,輸入
touch ~/.bash_profile

~/ 會指向使用者的家目錄。

接著開啟文字編輯器,並編輯剛剛創建的檔案。
nano ~/.bash_profile nano可以替換成 vim、vi 等等你習慣的編輯器,並輸入以下內容
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.2+9/Contents/Home

jdk-11.0.2+9 需替換成你所下載到的OpenJDK檔案名稱

如圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

按下 control+X ,並按下 y 來儲存檔案。

注意:若此處設定之後,更換到其他 shell 如 zsh ,建議將此內容也複製到新的 shell 環境之設定檔。

要使 .bash_profile 檔案生效,在終端機輸入
source ~/.bash_profile

接著在終端機輸入 /usr/libexec/java_home -V
會回傳目前系統中所有的 JDK 的版本、路徑等資訊,並顯示目前設定之JAVA_HOME目錄位置,若符合先前所設定的,那麼就大功告成了! 如圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Wed, Apr 3, 2019 4:15 PM