mac os java home,为什么我无法在MAC OS X 10.11上看到JAVA_HOME路径?

I am trying to run a JAVA application on my MAC OS X 10.11 ( El Capitan).

I used this link to set the JAVA_HOME on my MAC OS X 10.11.

I am able to run the following commands:

java -version

which java

However when I go to the Terminal app on MAC and type echo $JAVA_HOME, I see nothing.

Why is that?

Does that mean that my JAVA_HOME is not set properly ?

解决方案

Where to define it

Ok, first of all, we have to make clear where to set JAVA_HOME.

Simplified, you can define it in two files: either ~/.bashrc or ~/bash_profile. By default the former is executed for what is called "interactive non-login shells" while the latter is used for "login shells".

A "login shell" is exactly what you'd expect: a shell which is started after login via command line. An "interactive non-login shell" is a shell which is started from within a GUI for example. So, according to that, we should put our export statement into ~/.bashrc.

Side note: While OS X's "Terminal" application reads both files mentioned, this is not the default behavior and therefor should not be treated as such. And thats why I wrote an explanation.

What do define

You need to export JAVA_HOME in the ~/.bashrc file so that every time a shell is opened, the variable is set.

On OS X, the Java Development Kits and Runtime Environments are stored under /Library/Java/JavaVirtualMachines/ for quite a while now. Have a look there. This is how it looks at my machine:

/Library/Java/JavaVirtualMachines/

├── jdk1.7.0_45.jdk

├── jdk1.8.0_20.jdk

├── jdk1.8.0_25.jdk

└── jdk1.8.0_51.jdk

The subfolders look similar to this

jdk1.7.0_45.jdk/

└── Contents

├── Home

├── Info.plist

└── MacOS

And there we got it. So if you wanted to point to the JDK 1.7.0_45, you'd put the following statement into your .bashrc

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home"

To make sure that the correct binaries for that Java version are called, you also should add the following somewhere after the above statement:

export PATH=$JAVA_HOME/bin:$PATH

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值