如何在 Mac 上安装 Java 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24342886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:40:55  来源:igfitidea点击:

How to install Java 8 on Mac

javamacosjava-8installation

提问by user3763100

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

我想使用需要 Java 8 的最新 JavaFX 进行一些编程。我使用的是 IntelliJ 13 CE 和 Mac OS X 9 Mavericks。我运行了 Oracle 的 Java 8 安装程序,这些文件看起来像是在

/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 /Libraryinstead of /System/Library(nor what the difference is). But /usr/libexec/java_homedoesn'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 -Vstill only lists the old Java 1.6.

不知道为什么最新的安装程序把它放在/Library而不是/System/Library(也不知道有什么区别)。但是/usr/libexec/java_home没有找到 1.8,所以我找到的关于如何设置当前 Java 版本的所有帖子都不起作用。我试过添加一个符号链接,使它看起来像 1.8 在/System/Library...路径中,但它没有帮助。/usr/libexec/java_home -V仍然只列出旧的 Java 1.6。

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

具有讽刺意味的是,“系统偏好设置”下的“Java”控制面板仅显示 Java 1.8!

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

为什么 Oracle 的安装程序不把它放在真正需要的地方?我该如何解决这个问题?

回答by l3x

Oracle has a poor record for making it easy to install and configure Java, but using Homebrew, the latest OpenJDK (Java 13) can be installed with:

Oracle 在简化Java 安装和配置方面的记录不佳,但使用Homebrew,最新的 OpenJDK (Java 13) 可以通过以下方式安装:

brew cask install java

For the many use cases depending on an older version (commonly Java 8), the AdoptOpenJDKproject makes it possible with an extra step.

对于依赖于旧版本(通常是 Java 8)的许多用例,AdoptOpenJDK项目通过一个额外的步骤使其成为可能。

brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk8

Existing users of Homebrew may encounter Error: Cask adoptopenjdk8 exists in multiple tapsdue to prior workarounds with different instructions. This can be solved by fully specifying the location with brew cask install adoptopenjdk/openjdk/adoptopenjdk8.

Homebrew 的现有用户可能会遇到Error: Cask adoptopenjdk8 exists in multiple taps由于先前使用不同说明的变通方法。这可以通过使用 完全指定位置来解决brew cask install adoptopenjdk/openjdk/adoptopenjdk8

回答by Nathan Dunn

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

我有同时使用 Java 7 和 8 的应用程序,并且必须一直来回切换。

I use this script written by Johan:

我使用 Johan 编写的这个脚本:

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

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.

您现在可以在启动时设置它或之后调用脚本。
为 Mac 安装 JDK。

Java 7

爪哇 7

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

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

Java 8

爪哇 8

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

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



Updated recommendation!!:

更新推荐!!

Use jenv https://www.jenv.be/and homebrew.

使用 jenv https://www.jenv.be/和自制软件。

Then its just jenv global oracle6401.6.0.39and its set.

然后它的公正jenv global oracle6401.6.0.39和它的设置。

回答by kavehmb

Note: Oracle Java 8/9/10 is no longer available for public download (license change).

注意:Oracle Java 8/9/10 不再可供公共下载(许可证更改)。

First install and update brewfrom Terminal:

首先从终端安装和更新brew

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

brew tap homebrew/cask-versions

brew update

NEW as of June 2019

2019 年 6 月新增

To install the JDKs from AdoptOpenJDK:

要从 AdoptOpenJDK 安装 JDK:

brew tap adoptopenjdk/openjdk

brew cask install adoptopenjdk8
brew cask install adoptopenjdk9
brew cask install adoptopenjdk10
brew cask install adoptopenjdk11

OLD

老的

Java 8:

爪哇 8:

brew cask install java8

Java Latest:

Java最新:

brew cask install java

回答by toast38coza

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)

我开始真正喜欢在本地计算机上运行应用程序的一个选项是使用 Docker。您可以简单地在官方 JDK 容器中运行您的应用程序 - 这意味着您不必担心在本地机器上设置所有内容(或担心为不同的应用程序运行多个不同版本的 JDK 等)

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!

虽然这可能无法帮助您解决当前的安装问题,但它是一个解决方案,这意味着您可以避开与尝试让 Java 在您的开发机器上正确运行相关的问题雷区!

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.
  1. 无需在本地机器上设置任何版本的 Java(您只需在从 Docker Hub 拉取的容器中运行 Java)
  2. 只需更改容器上的标签即可轻松切换到不同版本的 Java。
  3. 项目依赖项安装在容器中 - 所以如果你弄乱了你的配置,你可以简单地核爆容器并重新开始。

A very simple example:

一个非常简单的例子:

Create a Dockerfile:

创建一个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/myappinside the container
  • 在这里,我们指定运行SDK的8版本的Java容器(java:8-使用Java 7,你可以只注明:java:7
  • 我们将本地目录映射到目录:/usr/src/myapp容器内

Create a docker-compose.ymlfile:

创建一个docker-compose.yml文件:

version: "2"

services:
  java:
    build: .
    volumes:
      - .:/usr/src/myapp

Now, assume we have this Java file:

现在,假设我们有这个 Java 文件:

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:

您可以执行各种 Java 操作,例如:

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
  • 您应该注意到 HelloWorld.class 显示在您的当前目录中(这是因为我们已将当前目录映射到我们代码所在的容器内的位置

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
  • -rmtells docker to remove the container once the command is finished running
  • javais 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.
  • 注意:第一次运行它会获取图像等。这需要一段时间 - 它只发生在第一次
  • docker-compose run- 从容器内运行命令
  • -rm一旦命令完成运行,告诉 docker 删除容器
  • java是将运行此命令的服务/容器的名称(来自我们的 docker-compose 文件)
  • 该行的其余部分是在容器内运行的命令。

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 :).

这是一种非常酷的方法,可以为不同的应用程序运行不同版本的 Java,而不会弄乱您的本地设置:)。

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

这是一个稍微复杂的例子,它有Maven 和一个简单的 Spring 应用程序

Disclaimer:

免责声明:

回答by Santhosh

Using brew

使用 brew

brew install Caskroom/cask/java

回答by sembian

Below steps worked for me.

以下步骤对我有用。

1) Uninstall all jdks

1)卸载所有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.

2) 安装苹果 jdk。

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

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

3)从Oracle下载最新的JDK并安装它,对我来说是JDK 1.82

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

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

Thats all it will work like a charm.

这就是它的魅力所在。

回答by sapy

Easiest way

最简单的方法

1) brew cask install java

2) java -version

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)

P.S - Cask is an extension to Homebrew that is intended to manage large Mac binaries and graphical applications, but using the Homebrew interface. Also see this answeron Apple StackExchange.

PS - Cask 是 Homebrew 的扩展,旨在管理大型 Mac 二进制文件和图形应用程序,但使用 Homebrew 界面。另请参阅Apple StackExchange 上的此答案

回答by Benedetto Abbenanti

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).

我有同样的问题要解决,因为我需要安装 JDK8 才能运行 Android SDK Manager(因为它似乎不适用于 JDK9)。但是,我告诉你我是如何在 Mac (Sierra) 上解决所有问题的。

First, you need brewwith cask and jenv.

首先,您需要使用caskjenv 进行brew

  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 java7or javaif 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 versionsto list all versions installed on your machine and then activate the one you want with jenv global [JDK_NAME_OF_LIST]
  1. 您可以在此处找到有用的指南Homebrew Cask Installation Guide。记得点击终端中运行的“caskroom/versions”: brew tap caskroom/versions
  2. 之后,使用以下命令安装 jenv: brew install jenv
  3. 安装任何你想要的版本桶 brew cask install java8(或java7或者java,如果你想安装最新版本,jdk9)
  4. 最后一步是配置要运行的版本(并让 jenv 管理您的 JAVA_HOME) jenv versions以列出您机器上安装的所有版本,然后激活您想要的版本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 Macand on Jenv Website

您可以在此 Github Gist brew-java-and-jenv.md 上、在此博客上在 Mac安装多个 JDK和在Jenv 网站上找到其他有用信息

回答by Jain Waldrip

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

我只是在我的 MBP 上做了这个,并且不得不使用

$ brew tap caskroom/versions
$ brew cask install java8

in order to get java8 to install.

为了让java8安装。

回答by nxhoaf

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:

如果您的机器上有多个 Java 版本,并且您想在运行时动态选择它,即,就我而言,我有两个版本:

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/profilecontent. Just add (or modify) the following two lines at the end of the file:

您可以通过修改/etc/profile内容来更改它们。只需在文件末尾添加(或修改)以下两行:

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:

爪哇7:

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

Java 8:

爪哇 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 runsource /etc/profileand it should work. Here are results when I use the first and second option accordingly:

保存文件后,请运行source /etc/profile,它应该可以工作。以下是我相应地使用第一个和第二个选项时的结果:

Java 7:

爪哇7:

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

Java 8:

爪哇 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.

如果您的 java 文件夹位于不同的位置,则过程类似。