简体   繁体   English

确保正确设置JAVA_HOME

[英]Making sure JAVA_HOME is correctly set

Pretty new to Java and also to Mac ... I want to make sure JAVA_HOME is set so in other programs I can use its path. 对Java和Mac来说都是新手...我想确保JAVA_HOME设置在其他程序中我可以使用它的路径。 So I did some Googling and here is what I got: 所以我做了一些谷歌搜索,这是我得到的:

If I enter /usr/libexec/java_home in terminal I get this: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home but if I enter echo $JAVA_HOME in terminal, I don't get anything back. 如果我在终端输入/ usr / libexec / java_home我得到: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home但是如果我在终端输入echo $ JAVA_HOME ,我什么也得不到。

Can you please tell me what is going on in here? 你能告诉我这里发生了什么吗?

Thanks. 谢谢。

JAVA_HOME isn't set by default on OSX. OSX上默认情况下未设置JAVA_HOME。 You can resolve this by opening terminal and executing the following: 您可以通过打开终端并执行以下操作来解决此问题:

echo "export JAVA_HOME=`/usr/libexec/java_home`" >> ~/.profile    
. ~/.profile

This will cause JAVA_HOME to be set on startup (rather than just the current session), and immediately add it. 这将导致JAVA_HOME在启动时设置(而不仅仅是当前会话),并立即添加它。

Checking JAVA_HOME path 检查JAVA_HOME路径

Try running source .bash_profile prior to echo $JAVA_HOME in your root directory. 尝试在根目录中的echo $JAVA_HOME之前运行source .bash_profile This should correct the problem if you've set JAVA_HOME correctly. 如果您正确设置了JAVA_HOME这应该可以解决问题。 If you're not sure you're in your root directory, simply type cd ~ , press enter and you're there. 如果你不确定你是否在你的根目录中,只需输入cd ~ ,按回车键就可以了。

根目录

  • Explanation : source loads and runs your bash_profile. 说明source加载并运行bash_profile。

If you haven't set JAVA_HOME correctly, following the instructions below should clear things up. 如果您没有正确设置JAVA_HOME ,请按照以下说明进行清理。

  • vim .bash_profileopens your bash_profile in Vim. vim .bash_profile - 在Vim中打开你的bash_profile。
    • I've included a list of VIM commands you'll likely need to edit your .bash_profile below. 我已经在下面列出了您可能需要编辑.bash_profile的VIM命令列表。

  • export JAVA_HOME=$(/usr/libexec/java_home)creates an ENV_VAR (Environment Variable) and sets/stores the home path of the JDK to (/usr/libexec/java_home) . export JAVA_HOME=$(/usr/libexec/java_home) - 创建一个ENV_VAR(环境变量)并将JDK的主路径设置/存储到(/usr/libexec/java_home)
  • Exit vim and type the following at the terminal* 退出vim并在终端输入以下内容*
  • source .bash_profileloads and runs your updated bash_profile source .bash_profile - 加载并运行更新的bash_profile
  • echo $JAVA_HOMEreturns the value stored in the ENV_VAR JAVA_HOME , which is the home path of your JDK installation. echo $JAVA_HOME - 返回存储在ENV_VAR JAVA_HOME的值,这是JDK安装的主路径。

VIM Commands: VIM命令:

Vim is an editor to create or edit a text file. Vim是一个用于创建或编辑文本文件的编辑器。 There are two modes in vim. vim中有两种模式。

  • Command Mode: user can move around the file, delete text, etc. 命令模式:用户可以移动文件,删除文本等。

  • Insert Mode: user can insert text. 插入模式:用户可以插入文本。

Changing between modes: 在模式之间切换:

Command mode to Insert mode 命令模式为插入模式

  • type the appropriate letter for the action you want (a, A, i, I, o, O) -- details for letters below. 为您想要的操作键入相应的字母(a,A,i,I,o,O) - 以下字母的详细信息。

Insert mode to Command mode 将模式插入命令模式

  • press Esc (escape key) 按Esc(退出键)

Text Entry Commands (Used to start text entry) 文本输入命令(用于启动文本输入)

  • a -- Append text following current cursor position a - 在当前光标位置后附加文本

  • A -- Append text to the end of current line A - 将文本附加到当前行的末尾

  • i -- Insert text before the current cursor position i - 在当前光标位置之前插入文本

  • I -- Insert text at the beginning of the cursor line I - 在光标行的开头插入文本

  • o -- Open up a new line following the current line and add text there o - 在当前行之后打开一个新行并在那里添加文本

  • O -- Open up a new line in front of the current line and add text there O - 在当前行前面打开一个新行并在那里添加文本

Cursor Movement Commands (only used in the commands mode.) 光标移动命令(仅在命令模式下使用。)

  • h -- Moves the cursor one character to the left h - 将光标向左移动一个字符

  • l -- Moves the cursor one character to the right l - 将光标向右移动一个字符

  • k -- Moves the cursor up one line k - 将光标向上移动一行

  • j -- Moves the cursor down one line j - 将光标向下移动一行

  • nG or :n -- Cursor goes to the specified (n) line nG或:n - 光标转到指定的(n)行

  • (ex. 10G goes to line 10) (例如10G转到第10行)

  • $ -- Move cursor to the end of current line $ - 将光标移动到当前行的末尾

  • 0 -- (zero) Move cursor to the beginning of current line 0 - (零)将光标移动到当前行的开头

  • w -- Forward one word w - 前进一个单词

  • b -- Backward one word b - 后退一个字

Exit Commands 退出命令

  • :wq -- Write file to disk and quit the editor :wq - 将文件写入磁盘并退出编辑器

  • :q! :Q! -- Quit (no warning) - 退出(没有警告)

  • :q -- Quit (a warning is printed if a modified file has not been saved) :q - 退出(如果尚未保存修改的文件,则会打印警告)

  • ZZ -- Save workspace and quit the editor (same as :wq) ZZ - 保存工作区并退出编辑器(与:wq相同)

VIM Editor Commands -- full list VIM编辑器命令 - 完整列表

The empty value of the echo command would mean that the value has not been set correctly as you are expecting. echo命令的空值意味着该值未按预期正确设置。 You can try creating/editing ~/.profile by adding the lines something like: 您可以尝试通过添加以下行来创建/编辑〜/ .profile:

vi ~/.profile
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home  

if you are not yet comfortable with vi editor , you may also create that file in your home directory using TextEdit program in Mac. 如果您对vi编辑器不熟悉,也可以使用Mac中的TextEdit程序在主目录中创建该文件。 and execute 并执行

source ~/.profile

That will load set the JAVA_HOME property. 那将加载设置JAVA_HOME属性。 Next you can try out executing echo $JAVA_HOME to check whether it has been set correctly as you expect. 接下来,您可以尝试执行echo $ JAVA_HOME以检查它是否已按预期正确设置。

You may also need to set PATH environment variable as something like: 您可能还需要将PATH环境变量设置为:

export PATH=PATH:$JAVA_HOME/bin

Edit your /etc/launchd.conf in your text editor. 在文本编辑器中编辑/etc/launchd.conf If it does not exist create it. 如果它不存在则创建它。

Then append the following line to the file: setenv JAVA_HOME /path/to/java/installation 然后将以下行附加到文件: setenv JAVA_HOME /path/to/java/installation

OR 要么

just type the following command in terminal 只需在终端中输入以下命令即可
sudo echo setenv JAVA_HOME /path/to/java/installation >> /etc/launchd.conf Then just enter your password when it prompts. sudo echo setenv JAVA_HOME /path/to/java/installation >> /etc/launchd.conf然后只需在提示时输入密码。

Now reboot your computer and the changes should have taken place. 现在重新启动计算机,应该进行更改。

Note : 注意 :
I am giving you advice based on my experience in Linux. 我根据自己在Linux上的经验给你建议。 But, these should work on MAC also. 但是,这些也应该适用于MAC。

Source 资源

This is not resilient to new installs of the JDK. 这对JDK的新安装没有弹性。 According to Apple: - you should not set it globally - you should use libexec 根据Apple的说法: - 你不应该全局设置它 - 你应该使用libexec

http://lists.apple.com/archives/java-dev/2011/May/msg00040.html http://lists.apple.com/archives/java-dev/2011/May/msg00040.html

You might think that: 你可能会认为:

$ cat /etc/launchd.conf setenv JAVA_HOME /usr/libexec/java_home $ cat /etc/launchd.conf setenv JAVA_HOME /usr/libexec/java_home

would work - but no, it sets it literally. 会工作 - 但不,它按字面意思设定。

But, as I say, according to that Apple Engineer, you are not supposed to set it globally anyway. 但是,正如我所说,根据Apple工程师的说法,你不应该在全球范围内设置它。 :( :(

I had this problem after setting JAVA_HOME with jenv. 用Jenv设置JAVA_HOME后我遇到了这个问题。 You can solve the problem by editing file 您可以通过编辑文件来解决问题

/Applications/Eclipse.app/Contents/Info.plist

Just set path to your version of java by uncommenting / editing appropriate part of file. 只需通过取消注释/编辑文件的相应部分来设置java版本的路径。 For AdoptOpenJDK installed with homebrew it looks like this: 对于与自制软件一起安装的AdoptOpenJDK,它看起来像这样:

<key>Eclipse</key>
<array>
    <string>-vm</string><string>/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin/java</string>
    <!-- to use a specific Java version (instead of the platform's default) uncomment one of the following options,
                   or add a VM found via $/usr/libexec/java_home -V
        <string>-vm</string><string>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands/java</string>
        <string>-vm</string><string>/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home/bin/java</string>
    -->
        <string>-keyring</string>
    <string>~/.eclipse_keyring</string>
</array>

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

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