How to install Java 8 on Mac

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I want to do some programming with the latest JavaFX, which requires Java 8. I'm using IntelliJ 13 CE and Mac OS X 9 Mavericks. I ran Oracle's Java 8 installer, and the files look like they ended up at

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk 

but previous versions are at

/System/Library/Java/JavaFrameworks/jdk1.6.... 

Not sure why the latest installer puts this in /Library instead of /System/Library (nor what the difference is). But /usr/libexec/java_home doesn't find 1.8, so all the posts I've found on how to set your current java version don't work. I've tried adding a symbolic link to make it look like 1.8 is in the /System/Library... path, but it doesn't help. /usr/libexec/java_home -V still only lists the old java 1.6.

Ironically, the "Java" control panel under System Preferences shows only java 1.8!

Why doesn't Oracle's installer put it where it really goes? And how can I work around this problem?

回答1:

Don't rely on Oracle to install Java properly on your Mac.

Use Homebrew:

brew update brew cask install java 

If you want to manage multiple versions of Java on your Mac, consider using jenv.

UPDATE: Now that Java 8 is no longer the most current version, we need to install it this way:

brew tap caskroom/versions brew cask install java8 

To get a list of all older versions of java: brew cask search java

We use brew cask since we'd otherwise use the Oracle GUI installer that will likely not install Java properly on your Mac. (Use brew cask install APP to install GUI apps; use brew install APP.) Java is not a GUI app; It should not require "cask" but at least Oracle is consistent.



回答2:

For El Capitan, Sierra and High Sierra

First install and update brew from Terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  brew tap caskroom/versions  brew update 

Java 8:

brew cask install java8 

Java 9 (Latest):

brew cask install java 


回答3:

I just did this on my MBP, and had to use

$ brew tap caskroom/versions $ brew cask install java8 

in order to get java8 to install.



回答4:

An option that I am starting to really like for running applications on my local computer is to use Docker. You can simply run your application within the official JDK container - meaning that you don't have to worry about getting everything set up on your local machine (or worry about running multiple different versions of the JDK for different apps etc)

Although this might not help you with your current installation issues, it is a solution which means you can side-step the minefield of issues related with trying to get Java running correctly on your dev machine!

The benefits are:

  1. No need to set up any version of Java on your local machine (you'll just run Java within a container which you pull from Docker Hub)
  2. Very easy to switch to different versions of Java by simply changing the tag on the container.
  3. Project dependencies are installed within the container - so if you mess up your config you can simply nuke the container and start again.

A very simple example:

Create a Dockerfile:

FROM java:8 COPY . /usr/src/myapp WORKDIR /usr/src/myapp 
  • Here we are specifying the Java container running version 8 of the SDK (java:8 - to use Java 7, you could just specify: java:7)
  • We are mapping the local directory with the directory: /usr/src/myapp inside the container

Create a docker-compose.yml file:

version: "2"  services:   java:     build: .     volumes:       - .:/usr/src/myapp 

Now, assume we have this Java file:

HelloWorld.java

public class HelloWorld {     public static void main(String[] args) {                 System.out.println("Hello, World");     } } 

So we have the following file structure:

. |_ Dockerfile |_ docker-compose.yml |_ HelloWorld.java 

You can do various Java things like:

compile:

docker-compose run --rm java javac HelloWorld.java  
  • You should note that the HelloWorld.class shows up in your current directory (this is cause we've mapped the current directory to the location inside the container where our code exists

run:

docker-compose run --rm java java HelloWorld  
  • Note: the first time you run this it will fetch the image etc. This will take a while - it only happens the first time
  • docker-compose run - runs a command from within the container
  • -rm tells docker to remove the container once the command is finished running
  • java is the name of the service/container (from our docker-compose file) against which this command will run
  • the rest of the line is the command to run inside the container.

This is quite a cool way of dealing with running different versions of Java for different apps without making a complete mess of your local setup :).

Here is a slightly more complex example which has Maven and a simple Spring app

Disclaimer:



回答5:

I'm having the same problem to solve, because I need to install JDK8 to run Android SDK Manager (because it seems that don't work well with JDK9). However, I tell you how I solve all problems on a Mac (Sierra).

First, you need brew with cask and jenv.

  1. You can find an useful guide here,Homebrew Cask Installation Guide. Remember to tap 'caskroom/versions' running in the terminal: brew tap caskroom/versions
  2. After that, install jenv with: brew install jenv
  3. Install whatever version you want with cask brew cask install java8 (or java7 or java if you want to install the latest version, jdk9)
  4. The last step is to configure which version to run (and let jenv to manage your JAVA_HOME) jenv versions to list all versions installed on your machine and then activate the one you want with jenv global [JDK_NAME_OF_LIST]

You could find other useful informations here on this Github Gist brew-java-and-jenv.md, on this blog Install multiple JDK on a Mac and on Jenv Website



回答6:

I have applications that use both Java 7 and 8 and have to go back and forth all the time.

I use this script written by Johan:

http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/

You can now set it at startup or call the script afterwards.
Install the JDK for Mac.

Java 7

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Java 8

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html



回答7:

Using brew

brew install Caskroom/cask/java 


回答8:

Easiest way -

1) brew cask install java (No need to install cask separately it comes with brew)  2) java -version  java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) 

P.S - What is brew-cask ? Homebrew-Cask extends Homebrew , and solves the hassle of executing an extra command - “To install, drag this icon…” after installing a Application using Homebrew.



回答9:

brew cask install caskroom/versions/java8



回答10:

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.



回答11:

Below steps worked for me.

1) Uninstall all jdks

In the Terminal window Copy and Paste the command below:

sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin  sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefpane 

2) Install APPLE jdk.

https://support.apple.com/kb/DL1572?locale=en_US

3) Download latest JDK from Oracle and install it , for me it was JDK 1.82

http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

Thats all it will work like a charm.



回答12:

I also had the same problem. But after little hit and trial, I was able to resolve the issue.

Try removing 1.6 sdk by sudo rm and restart your mac.

Download again the .dmg file. Chances are that the .dmg installer you downloaded, might be corrupt. Install again.

Run following command after installation. It gives path for jdk 8. /usr/libexec/java_home -v 1.8

Also you can run and see jdk 8 folder. The files may be hidden. ls -al /Library/Java/JavaVirtualMachines/



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!