ChatGPT解决这个技术问题 Extra ChatGPT

Installed Java 7 on Mac OS X but Terminal is still using version 6

I've installed JDK 7u7 downloaded from oracle's website. But after installation, the terminal is still showing java version 6

$java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

any idea why java 7 is not showing up?

Ans: OK, the problem has been resolved. Here is the answer: I found that my Terminal has a .bash_profile and the java home variable is set to 1.6

export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home"

So this is the line causing the problem every time I opened a new terminal window. Simply remove this line will solve the problem. You still need to follow what @aleroot said, but if that doesn't work for you, check the .bash_profile (or .bashrc) setting file to see if you've previously exported any java version.

I had this problem but I only updated the JRE and not the JDK. Once I updated the JDK it showed the correct version.
Migrate this to Ask Different?
Just to draw some extra attention to @Mazzy's comment, that's an easy mistake to make. Downloading JRE v7 will cause version 7 to appear when running the online version checker, or the Java control panel thingy, but when invoking Java from the command line you'll be running the older version from your previously installed JDK. So double-check to make sure you installed the JDK and not just the JRE.
The best way to solve this is to use the export JAVA_HOME approach, as detailed here in a slightly more flexible way.
Let me stress this in capital letters: MANY PEOPLE LANDING ON THIS PAGE WILL JUST NEED TO DOWNLOAD AND INSTALL THE LATEST JDK. THE JAVA CONTROL PANEL ONLY UPDATES THE JRE.

A
Anton Rudeshko

Oracle's installer puts java inside the /Library/Internet Plug-Ins/JavaAppletPlugin.plugin. And it doesn't overwrite /usr/bin/java. So, if you issue a

whereis java

in the terminal, it'll return /usr/bin/java. (which in turn points to /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java, which is Apple's 1.6 version).

So, if you want to use the new java version, replace the /usr/bin/java symlink so that it points to /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java instead:

sudo rm /usr/bin/java
sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin

Here is the command: sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin
Viktor's command works. But you may also need to be sure you remove the existing java symlink rm /usr/bin/java
Until Oracle folks improve this process, here's a HOWTO of the whole process of installing java on an osx system, up until and including this final symlink step: gist.github.com/johan/10590467
i got rm: /usr/bin/java: Operation not permitted
Or export PATH="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH"
j
j-beda

Install the JDK 7 and this problem will solve itself.

Be sure to get the Java Development Kit (JDK) which includes compilers and stuff like that, rather than just the Java Runtime Environment (JRE) .


instal the full JDK, not only the JRE.
Confirmed OK on OSX 10.9.4. It doesn't change the /usr/bin link, it changes stuff underneath it. So /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java # But: /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java -version java version "1.7.0_67"
Also worked for JDK 1.8 -- (install JDK 1.8 instead of JRE 1.8)
I tried to install Java 1.8, but that didn't work on El Capitan. Installing 1.7 solved my problems.
U
Uwe Günther

vi ~/.bash_profile

add

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

This tells your /usr/bin/java link target to use the latest Java 7 Package installed in

/Library/Java/JavaVirtualMachines/

So for JDK 1.7.0_17 JAVA_HOME would be:

/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home

Note: There were a lot of change in this area recently to move Mac OS X from Appels own System integrated Java packages to Oracle based Java Packages. The above solution is working fine as of Mac OS X 10.8.2


+1 for this answer which is imo the correct one because it works on versions of Mac OS before and after 'Java Preferences' was removed, and by showing the "-v" option it demonstrates how you can use java 6 even if java 7 is installed (which is what I want). Hence on my Mac running Lion (10.7.5) I use the same command with "-v 1.6". This is crucial because I need Java 6 and Oracle doesnt provide one for Mac OS - only Java 7, and this allows me to use the installed JAva 6 even though Java 7 is installed.
Also, to make it more flexible: mikemainguy.blogspot.com/2014/11/…
additional info can be found here for this awesome answer!
This worked for me in Sierra 10.12 after installing the 1.8 JKD the export worked by changing 1.7 to 1.8. Great answer using the java_home utility!!
For .cshrc setenv JAVA_HOME `/usr/libexec/java_home -v 15`
C
Community

Because you need to enter in Java Preferences pane and flag only the JVM 7 in this way :

https://i.stack.imgur.com/UeSQB.png

To easily and quickly open the Java Preferences pane in Mac OS X you can simply call spotlight with ⌘+SPACE and type System Preferences it will show up in the last row of the window.


yes, found it. but still version 1.6 in terminal, do i need to reboot or something
This panel does no longer exist in 10.8. Alternatives?
@Pepijn The panale is history as of Mac OSX 10.8.2 and there isn't really a need for it anymore. Instead you have to use export JAVA_HOME=`/usr/libexec/java_home -v 1.7` to switch to the latest Java 7 JDK from Oracle.
I think they got rid of Java Preferences at some point. It's not found by Spotlight on my box. Running 10.8.5.
You can find the exact location of JAVA_HOME under -> system preferences -> java. Click "Java" tab. Click view. The Path variable shows your JAVA_HOME (tile /Home).
d
dale.hamill

In my case, the issue was that Oracle was installing it to a different location than I was used to.

Download from Oracle: http://java.com/en/download/mac_download.jsp?locale=en

Verify that it's installed properly by looking in System Prefs: Command-Space to open Spotlight, type 'System Preferences', hit enter. Click Java icon in bottom row. After the Java Control Panel opens, click 'Java' tab, 'View...', and verify that your install worked. You can see a 'Path' there also, which you can sub into the commands below in case they are different than mine. Verify that the version is as you expect (sub in your path as needed): /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version Create link from /usr/bin/java to your new install sudo ln -fs /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin/java Sanity check your version: java -version


Worked for me too, I also tried the other explanation but with Maverix there is no panel where you can select a version number and also changing the .bash_profile didn't work. PERFECT!
Eclipse is not seeing 1.7 under compiler
Worked for me!. Before = 1.6.0_65 and After using this solution = 1.7.0_60.... thanks! :)
but eclipse gives error like still 'the jvm shared library does not contain the jni_createjavavm symbol' ....:(
Thanks for the help! Nothing else worked except this. Running Yosemite
l
lfender6445

I had run into a similar issue with terminal not updating the java version to match the version installed on the mac.

There was no issue with the JAVA_HOME environmental variable being set

I have come up with a temporary and somewhat painful but working solution.

In you .bash_profile add the line:

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

(This is the path on my machine but may be different on yours, make sure to get yours. The paths should match up to /Library/Java/JavaVirtualMachines/)

the run source ~/.bash_profile

As I mentioned this is a temporary band-aid solution because the java home path is being hard-coded. There is really no way to set the path to get the latest as that is what Apple is supposedly doing for terminal already and the issue is that Apple's java_home environment variable is not getting updated.


The directory is empty. $ ls /Library/Java/JavaVirtualMachines/
You probably have an issue with where you installed java 7. Please go to oracle's website. Click on the "Java SE Development Kit 7u11" link, accept the license agreement, and download "jdk-7u11-macosx-x64.dmg". Use the dmg to install Java 7 and this should put it in your JavaVirtualMachines directory.
This is one solution on Mojave 10.14.1 in early 2019
T
TabeaKischka

Since El Capitan, it is difficult to delete the /usr/bin/java symlink, because of the introduction of the new "rootless" policy.

Therefore, I simply added the path to the latest java version (in my case this is /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin) to the PATH in my .bashrc file:

# Use latest java version
export PATH=/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH

To reload your .bashrc file for the current session, run source ~/.bashrc in the shell. You only have to do this for sessions that had been started before changing the .bashrc file.

Now the latest version is used, when you use java in the shell.


This particular solution worked for me on my mac os x el capitan. I did not bypass the the security set up on el capitan, but installed java version 1.8. Doing export JAVA_HOME=/my specific path etc etc/ did not work (after going to preferences and locating the path to 1.8). Please note 1.6 is still installed. I had to create a .bashrc file and set the path as described here. I ran java -version and shazzam! it worked.
I so wanted this to work as I'm on El Capitan and can't get any other solution to function. I created a '.bashrc' file and added the line as above, but if I type 'java -version' I still get 1.6. Anything I might have overlooked?
@user3810073 maybe you need to run source .bashrc, in order to reload the contents of .bashrc
Worked for me. I added the line to .bash_profile (in the home directory of my user), then source .bash_profile. Then java -version gave me the latest one. Thanks!
This can also be achieved by symlinking to /usr/local/bin like this: sudo ln -sf "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java" /usr/local/bin/java That assuming /usr/local/bin is before /usr/bin in your $PATH which should be by default.
B
Bernhardt Scherer

Update

brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk/openjdk/adoptopenjdk8

https://stackoverflow.com/a/28635465

Old version For me the easiest and cleanest way to go is to install Java using homebrew like described here:

https://stackoverflow.com/a/28635465

brew update
brew cask install java

A
Amulya Kashyap

You can execute following command in your terminal :

export PATH="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH"

And this replaces the java old with new one.


Weirdly it replaces but then if I close terminal and open again, it again shows older version
thats true. so werid. very temporary solution
add that to .bashrc file then source ~/.bashrc to make it permanent
A
Aswin Kumar

The basic issue: /usr/bin/java is pointing to one provided by OSX itself initially (/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java) We need to point this to the one downloaded by the JDK installer. The below steps are for OSX 10.10.4 Yosemite.

Open System Preferences -> select Java. The Java window opens.

Click on Java tab at the top. Click on 'View' button.

The Java Runtime Environment Settings tab opens as below:

Double click on the Path item and copy the path (cmd+c). This is the latest one installed by the JDK installer/updater. In my case, the path was /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java

Open terminal. In this step, we are going to point (symbolic link, ln -s command) the system java binary to the latest one, which we discovered in the previous step. Run the below command:

sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin/java

Thats it. To verify, you can just run java -version on the terminal. It should output the latest version that you installed/updated to.


Works for me, except that I had to add -f to ln command. Otherwise if failed with ln: /usr/bin/java: File exists
in my case I'd to use usr/local/bin instead of usr/bin in terminal command
I am getting "operation not permitted".
Obtaining the path in this manner is ideal. I then was able to export it and reload bashrc to resolve the version issue. In my case I was migrating from version 7 to 8 on Mojave 10.14.1
C
Climbs_lika_Spyder

I did

export JAVA_HOME=`/usr/libexec/java_home`

and that fixed my Java 8 issue.

before:

java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

after:

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

The /usr/libexec/java_home is a very good indication, thanks!
Did the job for my after reading tons of possible solutions! Thanks :)
r
revher

http://www.java.com/en/download/faq/java_mac.xml is a nice place to understand, while Apple is stopping support to Java, why Java Preferences doesn't exist anymore and rely on System_Preferences => Java => Java if Java 7 from Oracle is installed.


This is good information, but you should really provide more details than just a link. If the link is broken in the future, your answer really won't be very useful.
T
TitiMoby

May I suggest you to have a look at the tool Jenv

This will allow you to switch at any time between your installed JVMs.

Simply as:

jenv global oracle-1.7

then later for test purpose:

jenv global oracle-1.6

And you have much more commands available.


+1 for this answer as you can utilize Homebrew, if you're using it already in your OS X system. Much cleaner than others I say. More info here: jenv.be
C
Community

Hard-coding the JAVA_HOME path might not be the best idea. The /usr/libexec/java_home utility sill works for me on Mac OSX Lion 10.7.5 and JDK 1.7.0_13. According to its man page, it is meant to be used with Apple's old Java Preferences pane, but it appears to be working with the replacement provided by Oracle (Java Control Panel). For example running /usr/libexec/java_home -V lists all installed JDKs (both 1.6.* and 1.7.*) on my machine.

So, as discussed in other threads (e.g. What should I set JAVA_HOME to on OSX), I would still recommend adding the following line to your .bash_profile:

export JAVA_HOME=$(/usr/libexec/java_home)

I would agree with your comment here, because that is best practice, but for some reason the java_home environment variable is not being set correctly which is why this does not work. I believe in Apple's latest update your suggestion is the default action for JAVA_HOME in terminal and other related applications by default.
@Derek What do you exactly mean on "the java_home environment variable is not being set correctly"? According to this article the /usr/libexec/java_home utility is meant to be working also with java 7 versions, and it returns the path to the most up-to-date JDK installed. It does perfectly work on my machine.
yes it is supposed to work however it does not for people with the problem that pepijin had mentioned.
P
Pupil

This is nuts! How does Oracle provide an installer that doesn't install anything!?

Anyways for me it was:

sudo rm /usr/bin/java
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre/bin/java /usr/bin/java

where 1.8.0_31 is your installed java version...


Why does Apple not provide a proper method?
H
Hoa Nguyen

In case if you have several Java versions on your machine and you want to choose it dynamically at runtime, i.e, in my case, I have two versions:

ls -la /Library/Java/JavaVirtualMachines
drwxr-xr-x  3 root  wheel    96B Nov 16  2014 jdk1.7.0_71.jdk/
drwxr-xr-x  3 root  wheel    96B Mar  1  2015 jdk1.8.0_31.jdk/

You can change them by modifying the /etc/profile content. Just add (or modify) the following two lines at the end of the file:

export JAVA_HOME=YOUR_JAVA_PATH/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

In my case, it should be like the following if I want to use:

Java 7:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

Java 8:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

After saving the file, please run source /etc/profile and it should work. Here are results when I use the first and second option accordingly:

Java 7:

java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)

Java 8:

java -version 
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)

The process is similar if your java folder is located in different locations.


C
Community

Simple Solution

export PATH="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH"

Awesome. This solution made my day. Thanks a lot Mick
D
DDDrop

I think you can try to install jdk not jre. By installing jdk, the java in /usr/bin/java will be replaced, and all library will work fine.


A
Andy Tran

If you have Homebrew installed, you can install java-switcher to change Java default version more quickly

brew tap andycillin/tap
brew install java-switcher

Then you can use just one command to switch your default Java version.

java-switcher 1.7

or

java-switcher 10

Thanks Andy, this was easy.
H
Hefewe1zen

You can run this command to find the version of Java that's under /Library/Internet Plugins/:

defaults read /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist CFBundleVersion

U
Uma Madhavi

It is happening because your .bash_profile is not reflecting changes.To reflect it, just use the following command

$ source .bash_profile

u
user1428716

Since i have not faced this issue , I am taking a hunch --

Can you please try this :

Where does the soft link "java_home" point to :

ls -lrt /usr/libexec/java_home

Output : (Stunted) lrwxr-xr-x java_home -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home

**ls -lrt /System/Library/Frameworks/JavaVM.framework/Versions My MAC Produces the following :

 lrwxr-xr-x CurrentJDK ->
 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents**

 lrwxr-xr-x   Current -> A
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.6.0 -> CurrentJDK
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.6 -> CurrentJDK
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.5.0 -> CurrentJDK
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.5 -> CurrentJDK
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.4.2 -> CurrentJDK
 lrwxr-xr-x  1 root  wheel   10 Oct 18 14:39 1.4 -> CurrentJDK

Based on this , we might get a hint to proceed further ?


read some commentaries on the Internet about Java 7 issues with MAC-OSX and it shows blocked.Refer this : grep.dk/tag/xprotect-meta-plist. In XProtect.meta.plist I have Java 7 and Flash listed.Until is playing the Vulnerability Police
A
AJAY

i resolved this issue by re installing Yosemite and then cross check java version on terminal (java -version) and (javac -version) .It work perfectly now.It is not changing to java 7 as version 6 still present on (command + n) libray>java>javavirtualmachine>your javac current version.you need to address to java home .


J
Jade Hamel

I resolved this issue with sudo rm /usr/bin/java

And I downloaded and installed the last Java SE Runtime Environment: http://www.oracle.com/technetwork/java/javase/downloads/index.html

sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre/bin/java /usr/bin/java did not work for me because I got Operation not permitted. El Capitan now protects certain system directories in "rootless" mode (a.k.a. System Integrity Protection). It is applicable to macOS Sierra, and probably new macOS versions for the foreseeable future.


S
Santanu Karar

Installing through distributed JDK DMG from the Oracle site auto-updates everything for me. I have seen (in El Capitan) updating through System Preferences > Java do updates but that do not reflect to command line. Installing through DMG does the job.


M
Miguel Mota

Check JDK versions installed:

$ ls  /Library/Java/JavaVirtualMachines/
jdk-11.0.2.jdk  jdk1.8.0_91.jdk

Now in your ~/.bashrc export JAVA_HOME specifying the version:

if [ -e /usr/libexec/java_home ]; then
  export JAVA_HOME=$(/usr/libexec/java_home -v 11)
fi

Source the bashrc file and the Java version will be updated:

$ java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

l
liangli

The latest 100% effective method:

in bash:

vim ~/.bash_profile

add

export PATH="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH"

:wq to save

cmd+q force quit bash

open bash again, and type in java -version

But actually this path points to jre not jdk.

If you want to point the path to JDK, you need

Make sure you have installed JDK not a single JRE runtime replace previous path to PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk", you can go to /Library/Java/JavaVirtualMachines to make sure you have installed the version of JDK you expected.