简体   繁体   English

如何在 Mac 中永久设置 JAVA_HOME?

[英]How to set JAVA_HOME in Mac permanently?

I am trying to set JAVA_HOME by entering export JAVA_HOME=/Library/Java/Home at terminal.我试图通过在终端输入export JAVA_HOME=/Library/Java/Home来设置JAVA_HOME It sets the JAVA_HOME for current session.它为当前会话设置JAVA_HOME

How can I set it permanently?如何永久设置?

You can use /usr/libexec/java_home -v <version you want> to get the path you need for JAVA_HOME .您可以使用/usr/libexec/java_home -v <version you want>获取JAVA_HOME所需的路径。 For instance, to get the path to the 1.7 JDK you can run /usr/libexec/java_home -v 1.7 and it will return the path to the JDK.例如,要获取 1.7 JDK 的路径,您可以运行/usr/libexec/java_home -v 1.7 ,它将返回 JDK 的路径。 In your .profile or .bash_profile just add在您的.profile.bash_profile添加

export JAVA_HOME=`/usr/libexec/java_home -v <version>`

and you should be good.你应该很好。 Alternatively, try and convince the maintainers of java tools you use to make use of this method to get the version they need.或者,尝试说服您使用的 Java 工具的维护者使用此方法来获取他们需要的版本。

To open '.bash_profile' type the following in terminal :要打开“.bash_profile”,请在终端中输入以下内容:

nano ~/.bash_profile 

and add the following line to the file:并将以下行添加到文件中:

export JAVA_HOME=`/usr/libexec/java_home -v <version>`

Press CTRL+X to exit the bash.按 CTRL+X 退出 bash。 Press 'Y' to save changes.按“Y”保存更改。

To check whether the path has been added, type following in terminal:要检查路径是否已添加,请在终端中键入以下内容:

source ~/.bash_profile
echo $JAVA_HOME

I was facing the same issue in MAC Catalina, If I edit .bash_profile i found export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home But When I run terminal echo $JAVA_HOME it was returning empty, Later I found that the file .zshrc was missing I created this file with我在 MAC Catalina 中遇到了同样的问题,如果我编辑 .bash_profile 我发现 export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home 但是当我运行终端 echo $JAVA_HOME 它返回空, 后来我发现文件 .zshrc 丢失了我用

touch .zshrc 

Then edit it by nano .zshrc and wrote然后通过nano .zshrc编辑它并写入

source ~/.bash_profile

Which solves my issue permanently这永久解决了我的问题

To set your Java path on mac:在 Mac 上设置 Java 路径:

  1. Open terminal on mac, change path to the root cd ~在mac上打开终端,将路径改为root cd ~
  2. vi .bash_profile (This opens the bash_profile file) vi .bash_profile(这将打开 bash_profile 文件)
  3. Click I to insert text and use the following text to set JAVA_HOME and PATH点击I插入文本并使用下面的文本设置JAVA_HOME和PATH

    • export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home'导出 JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home'
    • export PATH=$JAVA_HOME/bin:$PATH导出路径=$JAVA_HOME/bin:$PATH

      1. Type :wq to save and exit the file.输入 :wq 保存并退出文件。
      2. Type source .bash_profile to execute the .bash_profile file.键入 source .bash_profile 以执行 .bash_profile 文件。
      3. You can type echo $JAVA_HOME or echo $PATH您可以输入 echo $JAVA_HOME 或 echo $PATH

Installing Java on macOS 11 Big Sur :macOS 11 Big Sur上安装 Java:

  • the easiest way is to select OpenJDK 11 (LTS), the HotSpot JVM, and macOS x64 is to get the latest release here: adoptopenjdk.net最简单的方法是选择 OpenJDK 11 (LTS)、HotSpot JVM 和 macOS x64 是在此处获取最新版本: adaptopenjdk.net
  • Select macOS and x64 and download the JDK (about 190 MB), which will put the OpenJDK11U-jdk_x64_mac_hotspot_11.0.9_11.pkg file into your ~/Downloads folder选择 macOS 和 x64 并下载JDK (约 190 MB),这会将OpenJDK11U-jdk_x64_mac_hotspot_11.0.9_11.pkg文件放入~/Downloads folder
  • Clicking on pkg file, will install into this location: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk单击 pkg 文件,将安装到以下位置: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk 在此处输入图片说明
  • Almost done.快完成了。 After opening a terminal, the successful installation of the JDK can be confirmed like so: java --version打开终端后,可以像这样确认JDK安装成功: java --version
    • output:输出:
openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
  • JAVA_HOME is an important environment variable and it's important to get it right. JAVA_HOME是一个重要的环境变量,正确使用它很重要。 Here is a trick that allows me to keep the environment variable current, even after a Java Update was installed.这是一个技巧,即使在安装了 Java 更新之后,我也可以使环境变量保持最新。 In ~/.zshrc , I set the variable like so: export JAVA_HOME=$(/usr/libexec/java_home)~/.zshrc ,我像这样设置变量: export JAVA_HOME=$(/usr/libexec/java_home)
  • In previous macOS versions, this was done in ~/.bash_profile .在以前的 macOS 版本中,这是在~/.bash_profile Anyway, open a new terminal and verify: echo $JAVA_HOME无论如何,打开一个新终端并验证: echo $JAVA_HOME
    • output: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home输出: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

TEST: Compile and Run your Java Program测试:编译并运行您的 Java 程序

  • Open a text editor, copy the code from below and save the file as HelloStackoverflow.java .打开文本编辑器,从下面复制代码并将文件保存为HelloStackoverflow.java
public class HelloStackoverflow {
  public static void main(String[] args){
    System.out.println("Hello Stackoverflow !");
  }//End of main
}//End of HelloStackoverflow Class
  • From a terminal set the working directory to the directory containing HelloStackoverflow.java , then type the command:从终端将工作目录设置为包含HelloStackoverflow.java的目录,然后键入命令:
javac HelloStackoverflow.java
  • If you're lucky, nothing will happen如果你幸运的话,什么都不会发生

  • Actually, a lot happened.事实上,发生了很多事情。 javac is the name of the Java compiler. javac是 Java 编译器的名称。 It translates Java into Java Bytecode , an assembly language for the Java Virtual Machine (JVM).它将 Java 翻译成Java Bytecode ,Java 虚拟机 (JVM) 的一种汇编语言。 The Java Bytecode is stored in a file called HelloStackoverflow.class . Java 字节码存储在一个名为HelloStackoverflow.class的文件中。

  • Running: type the command:运行:输入命令:

java HelloStackoverflow

# output:
# Hello Stackoverflow !

在此处输入图片说明

Besides the settings for bash/ zsh terminal which are well covered by the other answers, if you want a permanent system environment variable for terminal + GUI applications (works for macOS Sierra; should work for El Capitan too):除了 bash/zsh 终端的设置(其他答案都很好地涵盖了这些设置),如果您想要一个用于终端 + GUI 应用程序的永久系统环境变量(适用于 macOS Sierra;也应该适用于 El Capitan):

launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 1.8)

(this will set JAVA_HOME to the latest 1.8 JDK, chances are you have gone through serveral updates eg javac 1.8.0_101, javac 1.8.0_131) (这会将 JAVA_HOME 设置为最新的 1.8 JDK,您可能已经经历了多个更新,例如 javac 1.8.0_101、javac 1.8.0_131)

Of course, change 1.8 to 1.7 or 1.6 (really?) to suit your need and your system当然,将 1.8 更改为 1.7 或 1.6(真的?)以满足您的需要和您的系统

Try this link http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/试试这个链接http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/

This explains correctly, I did the following to make it work这解释正确,我做了以下工作以使其工作

  1. Open Terminal打开终端
  2. Type vim .bash_profile输入vim .bash_profile
  3. Type your java instalation dir in my case export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home在我的例子中输入你的 java 安装目录export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
  4. Click ESC then type :wq (save and quit in vim)单击ESC然后键入:wq (在 vim 中保存并退出)
  5. Then type source .bash_profile然后输入source .bash_profile
  6. echo $JAVA_HOME if you see the path you are all set. echo $JAVA_HOME如果您看到所有设置的路径。

Hope it helps.希望能帮助到你。

This link may helps you: https://developer.apple.com/library/archive/qa/qa1067/_index.html此链接可能对您有所帮助: https : //developer.apple.com/library/archive/qa/qa1067/_index.html

Also, you can put the environment variable in one of these files:此外,您可以将环境变量放在以下文件之一中:

~/.bashrc
~/.bash_profile
~/.profile

run this command on your terminal(here -v11 is for version 11(java11))-:在您的终端上运行此命令(此处 -v11 适用于版本 11(java11))-:

/usr/libexec/java_home -v11

you will get the path on your terminal something like this -:你会在你的终端上得到这样的路径 -:

/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

now you need to open your bash profile in any editor for eg VS Code现在您需要在任何编辑器中打开您的 bash 配置文件,例如 VS Code

if you want to edit your bash_profile in vs code then run this command -:如果你想在 vs 代码中编辑你的 bash_profile 然后运行这个命令 -:

code ~/.bash_profile

else run this command and then press i to insert the path.否则运行此命令,然后按 i 插入路径。 -: -:

open ~/.bash_profile

you will get your .bash_profile now you need to add the path so add this in .bash_profile (path which you get from 1st command)-:您将获得 .bash_profile 现在您需要添加路径,因此将其添加到 .bash_profile (您从第一个命令获得的路径)中-:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

if you were using code editor then now go to terminal and run this command to save the changes -:如果您使用的是代码编辑器,那么现在转到终端并运行此命令以保存更改 -:

source ~/.bash_profile

else press esc then :wq to exit from bash_profile then go to terminal and run the command given above.否则按 esc 然后 :wq 退出 bash_profile 然后转到终端并运行上面给出的命令。 process completed.过程完成。 now you can check using this command -:现在您可以使用此命令进行检查 -:

echo $JAVA_HOME

you will get /Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home你会得到/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

To set JAVA_HOME permanently in Mac, I tried following steps.要在 Mac 中永久设置 JAVA_HOME,我尝试了以下步骤。

  1. Download and install Java JDK to your Mac.下载 Java JDK 并将其安装到您的 Mac。 When you install a Java JDK version which will be installed in the following location by default in MAC.当您安装 Java JDK 版本时,该版本将默认安装在 MAC 中的以下位置。

/Library/Java/JavaVirtualMachines /库/Java/JavaVirtualMachines

  1. Open the .bash_profile file (Here My Mac version is MacOS High Sierra. You may need to open .zshrc file in some different MacOS versions).打开.bash_profile文件(这里我的 Mac 版本是 MacOS High Sierra。您可能需要在某些不同的 MacOS 版本中打开.zshrc文件)。

atom ~/.bash_profile

  1. Add following to your bash_profile file.将以下内容添加到您的 bash_profile 文件中。

Change the JDK version accordingly相应地更改JDK版本

export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
  1. Open the Terminal and execute following.打开终端并执行以下操作。

source ~/.bash_profile

Open a new terminal and check 'echo $JAVA_HOME'打开一个新终端并检查'echo $JAVA_HOME'

Thanks.谢谢。

sql-surfer and MikroDel , sql- surferMikroDel ,

actually, the answer is not that complicated!其实,答案并没有那么复杂! You just need to add:您只需要添加:

export JAVA_HOME=(/usr/libexec/java_home)

to your shell profile/configuration file.到您的 shell 配置文件/配置文件。 The only question is - which shell are you using?唯一的问题是 - 您使用的是哪种外壳? If you're using for example FISH, then adding that line to .profile or .bash_profile will not work at all.例如,如果您使用的是 FISH,则将该行添加到.profile.bash_profile将根本不起作用。 Adding it to config.fish file though will do the trick.将它添加到config.fish文件中就可以了。 Permanently.永久。

1) The first step is to if you have Java installed and running your system. 1) 第一步是如果你安装了 Java 并运行你的系统。

which java

Usually, it should be /usr/bin/java.通常,它应该是/usr/bin/java。

2) JAVA_HOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java . 2) JAVA_HOME本质上是包含名为bin的子目录的目录的完整路径,该子目录又包含java .

cd /Library/Java/
  • You should have bin folder inside /Java folder and in turn it must contain the [java] executable package.您应该在/Java文件夹中有bin文件夹,并且它必须包含[java]可执行包。

Folder structure for Mac OS Mac OS 的文件夹结构

3.1) If you want to set the path only for the current session then execute this command in your terminal export JAVA_HOME=/Library/Java 3.1) 如果您只想为当前会话设置路径,则在您的终端中执行此命令export JAVA_HOME=/Library/Java

3.2) If you want it to persist, you will have to add the command to your ~/.bash_profile file 3.2) 如果你想让它持久化,你必须将命令添加到你的~/.bash_profile文件中

  • Open up Terminal vi ~/.bash_profile打开终端vi ~/.bash_profile
  • Add these lines to terminal export JAVA_HOME=/Library/Java/Home and save it将这些行添加到终端export JAVA_HOME=/Library/Java/Home并保存
  • Execute below command source ~/.bash_profile执行以下命令source ~/.bash_profile

4) Verify you have correctly added JAVA_HOME path. 4) 验证您是否正确添加了 JAVA_HOME 路径。 Below command should give you the proper Java version.下面的命令应该给你正确的 Java 版本。

That's it and your ready to use!!就是这样,您可以使用了!!

First, figure out where your java home is by running the command /usr/libexec/java_home -v <version> replacing with whatever version of OpenJDK your running.首先,通过运行命令/usr/libexec/java_home -v <version>替换您运行的任何版本的 OpenJDK,找出您的 java home 所在的位置。

Next use vim ~/.bash_profile to edit your bash profile.接下来使用vim ~/.bash_profile编辑您的 bash 配置文件。 Add export JAVA_HOME="<java path>" replacing with the path to your java home found in the last step.添加export JAVA_HOME="<java path>"替换为您在上一步中找到的 java home 的路径。

Finally, run the command source ~/.bash_profile最后,运行命令source ~/.bash_profile

This should permanently set your JAVA_HOME environment variable.这应该永久设置您的 JAVA_HOME 环境变量。

To make sure it worked run echo $JAVA_HOME and make sure it returns the path you set为了确保它工作运行echo $JAVA_HOME并确保它返回您设置的路径

添加到 Dilips 的答案中,如果您使用的是 JDK 9,请在步骤 3 中使用以下内容(我的 JDK 版本为 9.0.4):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home

Declare two export inside your .bashrc or .zshrc:在 .bashrc 或 .zshrc 中声明两个导出:

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) 
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

Add alias for quick change:添加别名以进行快速更改:

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'

set default to Java 11将默认值设置为 Java 11

java11

export PATH导出路径

export PATH=$JAVA_HOME/bin:$PATH

you could change java11 by java8 inside your .bashrc/zshrc file to change permanently your java version您可以在 .bashrc/zshrc 文件中通过 java8 更改 java11 以永久更改您的 java 版本

to set JAVA_HOME permenantly in mac make sure you have JDK installed in your system, if jdk is not installed you can download it from here https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html在 mac 中永久设置 JAVA_HOME 确保您的系统中安装了 JDK,如果未安装 jdk,您可以从这里下载https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 .html

After installing jdk follow these steps :-安装 jdk 后,请按照下列步骤操作:-

1) Open Terminal 1) 打开终端

2) Type "vim .bash_profile" 2) 输入“vim .bash_profile”

3) press "i" to edit or enter the path 3)按“i”编辑或输入路径

4) Type your java instalation dir :- export JAVA_HOME=$(/usr/libexec/java_home) 4) 输入你的 java 安装目录:- export JAVA_HOME=$(/usr/libexec/java_home)

5) Click ESC then type ":wq" (save and quit in vim) 5)点击ESC然后输入“:wq”(在vim中保存并退出)

6) Then type "source .bash_profile" 6) 然后输入“source .bash_profile”

7) type "echo $JAVA_HOME" if you see the path you are all set. 7) 如果您看到已设置好的路径,请输入“echo $JAVA_HOME”。

THANK YOU谢谢

If you are using the latest versions of macOS, then you cannot use ~/.bash_profile to export your environment variable since the bash shell is deprecated in the latest version of macOS.如果您使用的是最新版本的 macOS,则无法使用 ~/.bash_profile 导出环境变量,因为 bash shell 在最新版本的 macOS 中已弃用。

  • Run /usr/libexec/java_home in your terminal and you will get things like /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home在你的终端中运行/usr/libexec/java_home ,你会得到类似/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home东西
  • Add export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home to .zshrcexport JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Homeexport JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home

If you are using fish shell.如果您使用的是鱼壳。 Then all the variables can be set in .config/fish/config.fish然后所有的变量都可以在.config/fish/config.fish中设置

vim .config/fish/config.fish

Add the following lines添加以下几行

set -g JAVA_HOME "your_path_to_jdk"

save and exit out of vim.保存并退出vim。

This should be setting your JAVA_HOME.这应该是设置您的 JAVA_HOME。 Thanks谢谢

Terminal.app is a terminal emulator. Terminal.app是一个终端模拟器。 It executes your shell, bash by default. 它执行您的shell,默认情况下为bash。 Put your command in .bashrc in your home folder and any new sessions should pick that up. 将命令放在主文件夹中的.bashrc中,任何新的会话都应执行此操作。 For more information on bash, look at the manual . 有关bash的更多信息,请参阅手册

Find out the jenv will set the $JAVA_HOME a short version like "/Users/*****/.jenv/versions/1.8".找出 jenv 会将 $JAVA_HOME 设置为一个简短的版本,例如“/Users/*****/.jenv/versions/1.8”。

Have to uninstall jenv to make the $JAVA_HOME setting works like "JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home"必须卸载 jenv 才能使 $JAVA_HOME 设置像“JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home”一样工作

the answers here are in general correct, but for me I didn't know that I need to do something like source ~/.bash_profile to be able to really make it work.这里的答案通常是正确的,但对我来说,我不知道我需要做一些类似source ~/.bash_profile事情才能真正让它工作。

so the full answer is:所以完整的答案是:

nano ~/.zshenv // or
nano ~/.zshrc // or
nano ~/.bash_profile
//add this line to zshrc and bash_profile if you want to be 100% sure
export JAVA_HOME=$(/usr/libexec/java_home)
//save
source ~/.bash_profile //the KEY that generally isn't in the answers
source ~/.zshrc //I didn't do this one, but maybe you need it

add following添加以下

setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home

in your ~/.login file:在您的 ~/.login 文件中:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM