问题
I am trying to set JAVA_HOME
by entering export JAVA_HOME=/Library/Java/Home
at terminal.
It sets the JAVA_HOME
for current session.
How can I set it permanently?
回答1:
I know it has been a while for this question, but I just wanted to share the following piece of information that I came across today.
You can use /usr/libexec/java_home -v <version you want>
to get the path you need for 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. In your .profile
or .bash_profile
just add
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.
To open '.bash_profile' type the following in terminal :
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. Press 'Y' to save changes.
To check whether the path has been added, type following in terminal:
source ~/.bash_profile
echo $JAVA_HOME
回答2:
This link may helps you: http://developer.apple.com/library/mac/#/legacy/mac/library/qa/qa1067/_index.html
Also, you can put the environment variable in one of these files:
~/.bashrc
~/.bash_profile
~/.profile
回答3:
Try this link 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
- Open Terminal
- Type
vim .bash_profile
- Type your java instalation dir in my case
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
- Click
ESC
then type:wq
(save and quit in vim) - Then type
source .bash_profile
echo $JAVA_HOME
if you see the path you are all set.
Hope it helps.
回答4:
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):
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 e.g. 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
回答5:
To set your Java path on mac:
- Open terminal on mac, change path to the root cd ~
- vi .bash_profile (This opens the bash_profile file)
Click I to insert text and use the following text to set JAVA_HOME and PATH
- export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
- Type :wq to save and exit the file.
- Type source .bash_profile to execute the .bash_profile file.
- You can type echo $JAVA_HOME or echo $PATH
回答6:
Adding to Dilips's answer, if you are working with JDK 9, use the following (my JDK version is 9.0.4) in Step # 3:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
回答7:
sql-surfer and MikroDel,
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. 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. Adding it to config.fish
file though will do the trick. Permanently.
回答8:
Terminal.app
is a terminal emulator. It executes your shell, bash by default. Put your command in .bashrc in your home folder and any new sessions should pick that up. For more information on bash, look at the manual.
回答9:
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
touch .zshrc
Then edit it by nano .zshrc
and wrote
source ~/.bash_profile
Which solves my issue permanently
回答10:
add following
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
in your ~/.login file:
回答11:
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
After installing jdk follow these steps :-
1) Open Terminal
2) Type "vim .bash_profile"
3) press "i" to edit or enter the path
4) Type your java instalation dir :- export JAVA_HOME=$(/usr/libexec/java_home)
5) Click ESC then type ":wq" (save and quit in vim)
6) Then type "source .bash_profile"
7) type "echo $JAVA_HOME" if you see the path you are all set.
THANK YOU
来源:https://stackoverflow.com/questions/14702702/how-to-set-java-home-in-mac-permanently