简体   繁体   English

如何在 Mac OSX 上使用 JDK 7?

[英]How do I use JDK 7 on Mac OSX?

I would like to use the WatchService API as mentioned in this link: http://download.oracle.com/javase/tutorial/essential/io/notification.html I would like to use the WatchService API as mentioned in this link: http://download.oracle.com/javase/tutorial/essential/io/notification.html

After reading around, I found out that WatchService is part of the NIO class which is scheduled for JDK 7. So, it is in beta form.阅读后,我发现 WatchService 是为 JDK 7 计划的 NIO class 的一部分。因此,它是 beta 形式。 It's fine.没关系。

http://jdk7.java.net/download.html has the JDK which I downloaded and extracted. http://jdk7.java.net/download.html有我下载并解压的JDK。 I got a bunch of folders.我有一堆文件夹。 I don't know what to do with them.我不知道该怎么处理他们。

Then, I read around some more and found that some nice group of people created JDK 7 as a binary so someone like me can install it easily.然后,我阅读了更多内容,发现一些不错的人将 JDK 7 创建为二进制文件,因此像我这样的人可以轻松安装它。 It is called Open JDK: http://code.google.com/p/openjdk-osx-build/它被称为 Open JDK: http://code.google.com/p/openjdk-osx-build/

So, I downloaded the.dmg file and install it.所以,我下载了 .dmg 文件并安装它。 Then I open "Java Preference" and see that OpenJDK7 is available.然后我打开“Java Preference”,看到 OpenJDK7 可用。

So, now I feel that I can start trying out WatchService API.所以,现在感觉可以开始试用WatchService API了。 From the tutorial in the first link, the author gave a.java file to test it out first and make sure that it is running.从第一个链接的教程中,作者给出了一个.java 文件,首先测试它并确保它正在运行。 Here is the link to the file: http://download.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java这是文件的链接: http://download.oracle.com/javase/tutorial/essential/io/examples/WatchDir.Z93F725A07423FE1C889F448B33D21F46

So, I boot up Eclipse (actually I use STS) and create a new Java project and choose JaveSE-1.7 in the "use an execution environment JRE:".于是,我启动Eclipse(其实我用的是STS),新建一个Java项目,在“使用执行环境JRE:”中选择JaveSE-1.7。 Under the src folder, I copy pasted the WatchDir.java file.在 src 文件夹下,我复制粘贴了 WatchDir.java 文件。

And I still see tons of squiggly red lines.而且我仍然看到大量弯曲的红线。 All the "import.java.nio.*" are all red and I cannot run it as a Java app.所有“import.java.nio.*”都是红色的,我无法将其作为 Java 应用程序运行。

What do I need to do?我需要做什么?

This is how I got 1.7 to work with Eclipse.这就是我如何让 1.7 与 Eclipse 一起工作。 I hope it helps.我希望它有所帮助。

  1. I Downloaded the latest OpenJDK 1.7 universal (32/64 bits) JDK from Mac OS/X branch from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html I Downloaded the latest OpenJDK 1.7 universal (32/64 bits) JDK from Mac OS/X branch from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
  2. copied the jdk to /Library/Java/JavaVirtualMachines/ next to the default 1.6.0 one把jdk复制到/Library/Java/JavaVirtualMachines/旁边默认1.6.0一个
  3. In Eclipse > Preferences > Java > Installed JREs you add a new one, of type MacOS X VM, and set the home as /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home and name Java SE 7 (OpenJDK) In Eclipse > Preferences > Java > Installed JREs you add a new one, of type MacOS X VM, and set the home as /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home and name Java SE 7 (OpenJDK)
  4. Click Finish点击完成
  5. Set the added JRE as default将添加的 JRE 设置为默认值

that should be it:)应该是这样的:)

Oracle has released JDK 7 for OS X . Oracle 已发布适用于 OS X 的 JDK 7

Java 9, 10, 11 Java 9、10、11

Years ago, Apple joined the OpenJDK project, transferring their formerly proprietary macOS-specific JVM code as free-of-cost open-source.多年前,Apple 加入了OpenJDK项目,将其以前专有的 macOS 专用 JVM 代码免费开源。 Apple ceased distribution of their own branded JVM/JDK, relying on Oracle's branded releases to include a version for macOS. Apple 停止分发自己品牌的 JVM/JDK,依靠 Oracle 的品牌版本来包含 macOS 版本。

Oracle has recently announced their intention to bring their Oracle-branded JVM release to feature parity with the OpenJDK project, with virtually the same code base. Oracle 最近宣布,他们打算将其 Oracle 品牌的 JVM 版本与 OpenJDK 项目具有同等的功能,并具有几乎相同的代码库。 The company even donated their previously commercial tools, Flight Recorder & Mission Control , to the OpenJDK project.该公司甚至将他们以前的商业工具Flight Recorder & Mission Control捐赠给了 OpenJDK 项目。 This is part of the shift to a new rapid “release train” plan for predictably scheduled versioning of Java and OpenJDK.这是向新的快速“发布火车”计划转变的一部分,该计划可预测 Java 和 OpenJDK 的预定版本控制。 Notably, the periods for free-of-cost public updates is now shortened.值得注意的是,免费公开更新的时间现在缩短了。 See this 2017-09 announcement and this posting by Mark Reinhold .请参阅此 2017-09 公告Mark Reinhold 的此帖子

As a result of all this, macOS users of Java have a choice of vendors for a Java implementation.因此,Java 的 macOS 用户可以选择 Java 实施的供应商。 At this point, at least three sources are based on OpenJDK for macOS:至此,至少有三个源是基于 OpenJDK for macOS 的:

  • Oracle releases of the JDK and JRE, with optional paid support. JDK 和 JRE 的Oracle 版本,具有可选的付费支持。
  • Azul Systems releasing: Azul Systems发布:
    • Zulu line of free-of-cost JVMs with optional paid support. Zulu系列免费 JVM,具有可选的付费支持。
    • Zing line of commercial JVMs with special features such as an alternate garbage-collector. Zing系列商业 JVM 具有特殊功能,例如备用垃圾收集器。
  • OpenJDK source code , roll-your-own compilation & installation (perhaps not practical for most of us). OpenJDK 源代码,自行编译和安装(可能对我们大多数人来说不实用)。

Meanwhile, IBM donated code for a JVM to the Eclipse Foundation, now housed in the OpenJ9 project.同时,IBM 将 JVM 的代码捐赠给了 Eclipse 基金会,该基金会现在位于OpenJ9项目中。 I wonder if they might support a macOS release as well, though it is too soon to tell.我想知道他们是否也可能支持 macOS 版本,尽管现在说还为时过早。

Personally, I am currently using the Zulu release of Java 10.0.1 from Azul on macOS High Sierra successfully with IntelliJ 2018.2 to produce Java-backed web apps with Vaadin .就个人而言,我目前正在使用来自 Azul 的 Java 10.0.1 的Zulu版本在 macOS High Sierra 上成功地与IntelliJ 2018.2 一起使用Vaadin生成 Java 支持的 web 应用程序。

Installation/Removal安装/拆卸

Both Oracle and Azul provide utterly easy-to-use installers to install the JVM/JDK on your Mac. Oracle 和 Azul 都提供了非常易于使用的安装程序来在您的 Mac 上安装 JVM/JDK。 Verify your installation by using the Terminal.app (or equivalent) to type and run:使用 Terminal.app(或等效的)输入并运行来验证您的安装:

java -version

You will find the Java installations in this folder at the root level of your drive (not in your home folder):您将在驱动器根目录的此文件夹中找到 Java 安装(不在您的主文件夹中):

/Library/Java/JavaVirtualMachines /库/Java/JavaVirtualMachines

Each version from each vendor is found there, in a labeled nested folder.每个供应商的每个版本都在那里,在一个标记的嵌套文件夹中。 You can delete any installation simply by deleting the nested folder for that version and providing your system password when prompted.您只需删除该版本的嵌套文件夹并在出现提示时提供系统密码即可删除任何安装。

Java 8 Java 8

You can download the Java Development Kit (JDK) for Java 8 for the supported versions of Mac OS X:您可以下载适用于 Mac OS X 支持版本的Java 8 的 Java 开发工具包 (JDK)

  • Mountain Lion (10.8.3+)山狮 (10.8.3+)
  • Mavericks (10.9)小牛队(10.9)
  • Yosemite (10.10)优胜美地 (10.10)
  • El Capitan (10.11)埃尔卡皮坦 (10.11)

Each version of JVM you install can be found here:您安装的每个版本的 JVM 都可以在这里找到:

/Library/Java/JavaVirtualMachines

For more instructions and FAQ, see this Oracle Guide .有关更多说明和常见问题解答,请参阅此 Oracle 指南

Java 7 Java 7

For every release of Java 7 since Update 4, a Mac version has been ready alongside the other platforms.对于自更新 4 以来Java 7的每个版本,Mac 版本已与其他平台一起准备好。 Runs on Macs with 64-bit hardware on Lion (10.7.3+), Mountain Lion (10.8.3+), and Mavericks (10.9.x).在具有 64 位硬件的 Mac 上运行 Lion (10.7.3+)、Mountain Lion (10.8.3+) 和 Mavericks (10.9.x)。

Oracle announced the official release of the JDK for Java SE 7 Update 4 on Mac OS X (Lion), as of 2012-04-26. Oracle 于 2012 年 4 月 26 日宣布正式发布适用于 Mac OS X (Lion) 上的 Java SE 7 Update 4 的 JDK。 No more need for the tricks discussed on this page.不再需要本页讨论的技巧

Installation is simple per these instructions :按照这些说明安装很简单:

  1. Download from the usual place on the Oracle web site.从 Oracle web 站点上的常规位置下载。
  2. Mount the DMG.安装 DMG。
  3. Run the installer.运行安装程序。

This release has a few limitations, most notably the lack of support for Java Web Start and the Java Plugin for web browsers. This release has a few limitations, most notably the lack of support for Java Web Start and the Java Plugin for web browsers. That support is expected later this year.预计将在今年晚些时候获得这种支持。

After installing, read the JDK for Mac ReadMe.安装后,阅读JDK for Mac 自述文件。 Most importantly, if you want Java 7 to be the default, drag it to the top of the list in the Java Preferences app found in your Utilities folder.最重要的是,如果您希望 Java 7 成为默认值,请将其拖到 Utilities 文件夹中的Java Preferences应用程序的列表顶部。

Mac OS X easily supports multiple JVMs simultaneously. Mac OS X 可以轻松同时支持多个 JVM。 Each is now found here:现在每个都可以在这里找到:

/Library/Java/JavaVirtualMachines

Congratulations to the Apple & Oracle teams for their achievement.祝贺 Apple & Oracle 团队取得的成就。 This geek gets a thrill seeing Mac OS X listed as a " Certified System Configuration ".这位极客看到 Mac OS X 被列为“ 认证系统配置”而激动不已。

Tip: To start Eclipse on a Mac with only Java 7 installed, open the alias file named eclipse rather than the file named Eclipse.app . Tip: To start Eclipse on a Mac with only Java 7 installed, open the alias file named eclipse rather than the file named Eclipse.app .

Java 6 Java 6

Apple continues to supply an up-to-date implementation of Java 6 for all versions of Mac OS X up through Mountain Lion. Apple 继续为一直到 Mountain Lion 的所有 Mac OS X 版本提供 Java 6 的最新实施。

If you do something that requires Java, such as type "java -version" in Terminal.app, a dialog appears offering to install Java for you.如果您执行需要 Java 的操作,例如在 Terminal.app 中键入“java -version”,则会出现一个对话框,为您安装 Java。 If you accept, installation happens automatically similar to other "Software Updates" from Apple.如果您接受,安装会自动进行,类似于 Apple 的其他“软件更新”。

You will find Java installed in this location, different than Java 7 & 8:您会发现 Java 安装在此位置,与 Java 7 和 8 不同:

/System/Library/Java/JavaVirtualMachines

Java 6 has reached end-of-life with Oracle as of 2013-02 (unless you have a commercial support agreement with Oracle).自 2013 年 2 月起,Java 6 与 Oracle的生命周期已结束(除非您与 Oracle 签订了商业支持协议)。 So you should be moving to Java 7 or 8.所以你应该转移到 Java 7 或 8。

Testing New Version测试新版本

In Terminal.app, type java -version to verify which version is the current default.在 Terminal.app 中,键入java -version以验证哪个版本是当前默认版本。

Deleting Old Versions删除旧版本

After installing a fresh version, you may want to visit the folder described above to delete old versions.安装新版本后,您可能需要访问上述文件夹以删除旧版本。 Move the folder to the Trash, and provide your System password complete the move.将文件夹移至废纸篓,并提供您的系统密码以完成移动。


By the way, Apple provides a mailing list for developers' technical issues related to Java on OS X.顺便说一句,Apple 为开发人员在 OS X 上与 Java 相关的技术问题提供了一个邮件列表

An easy way to install Java 7 on a Mac is by using Homebrew , thanks to the Homebrew Cask plugin (which is now installed by default).在 Mac 上安装 Java 7 的一种简单方法是使用Homebrew ,这要归功于Homebrew Cask插件(现在默认安装)。

Run this command to install Java 7:运行此命令安装 Java 7:

brew cask install caskroom/versions/java7

Get cask获取木桶

brew tap caskroom/cask

Install java7:安装java7:

brew tap caskroom/versions
brew cask install java7

(I had difficulty finding the download link of java7 on oracle website, as they're just "recommending" java8 ) (我很难在 oracle 网站上找到 java7 的下载链接,因为他们只是“推荐” java8 )

EDIT January 2018 (As pointed by Ankur ):编辑 2018 年 1 月(由Ankur指出):

Use zulu7 cask.使用zulu7木桶。 Zulu is a certified build of OpenJDK produced by Azul Systems that should be around for a long time (they even offer JDK6 builds still). Zulu 是由 Azul Systems 生产的经过认证的 OpenJDK 构建,应该存在很长时间(他们甚至仍然提供 JDK6 构建)。

brew cask install caskroom/versions/zulu7

I know that some may want to smack me for re-opening old post, but if you feel so do it I just hope this may help someone else trying to set JDK 7 on Mac OS (using IntelliJ).我知道有些人可能会因为我重新打开旧帖子而打我,但如果你觉得这样做,我只是希望这可以帮助其他尝试在 Mac OS 上设置 JDK 7(使用 IntelliJ)的人。

What I did to get this working on my machine is to:我为在我的机器上工作所做的工作是:

  • followed instructions on Oracle JDK7 Mac OS X Port for general installation按照Oracle JDK7 Mac OS X Port的说明进行常规安装
  • in IntelliJ open/create new project so you can add new SDK (File > Project Structure)在 IntelliJ 打开/创建新项目,以便您可以添加新的 SDK(文件 > 项目结构)
  • select Platform Settings > SDKs, press "+" (plus) sign to add new SDK select 平台设置 > SDK,按“+”(加号)添加新的 SDK
  • select JSDK and navigate to /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home. select JSDK 并导航到 /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home。 Do not get it mistaken with /Users/YOUR_USERNAME/Library/Java/ .不要误认为/Users/YOUR_USERNAME/Library/Java/ This will link 4 JARs from "lib" directory (dt.jar, jconsole.jar, sa-jdi.jar and tools.jar) This will link 4 JARs from "lib" directory (dt.jar, jconsole.jar, sa-jdi.jar and tools.jar)
  • you will need also add JARs from /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib (charsets.jar, jce.jar, JObjC.jar, jsse.jar, management-agent.jar, resources.jar and rt.jar) you will need also add JARs from /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib (charsets.jar, jce.jar, JObjC.jar, jsse.jar, management-agent .jar, 资源.jar 和 rt.jar)

after installing the 1.7jdk from oracle, i changed my bash scripts to add:从 oracle 安装 1.7jdk 后,我将 bash 脚本更改为添加:

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

and then running java -version showed the right version.然后运行java -version显示正确的版本。

It's possible that you still need to add the JDK into Eclipse (STS).您可能仍需要将 JDK 添加到 Eclipse (STS) 中。 Just because the JDK is on the system doesn't mean Eclipse knows where to find it.仅仅因为 JDK 在系统上并不意味着 Eclipse 知道在哪里可以找到它。

Go to Preferences > Java > Installed JREs Go 到首选项 > Java > 安装的 JRE

If there is not an entry for the 1.7 JDK, add it.如果没有 1.7 JDK 的条目,请添加它。 You'll have to point Eclipse to where you installed your 1.7 JDK.您必须将 Eclipse 指向您安装 1.7 JDK 的位置。

If Eclipse can't find a JRE that is 1.7 compatible, I'm guessing that it just uses your default JRE, and that's probably still pointing at Java 1.6, which would be causing your red squiggly lines.如果 Eclipse 找不到兼容 1.7 的 JRE,我猜它只是使用您的默认 JRE,并且可能仍然指向 Java 1.6,这将导致您的红色波浪线。

As of April 27th there is an offical Oracle release of Java SE 7u4.截至 4 月 27 日, 官方发布了 Java SE 7u4 的 Oracle 版本。 Download the disk image and run the installer - then see the Mac readme .下载磁盘映像并运行安装程序 - 然后查看Mac 自述文件。

As of December 2017, previously posted links don't work, but JDK 7 can still be downloaded from Oracle Archives (login required):截至 2017 年 12 月,之前发布的链接已失效,但仍可从 Oracle 存档下载 JDK 7(需要登录):

http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.ZFC35FDC70D5FC69D2639883A822C7A5

How about Netbeans, here is an article how to set it up with NB7: Netbeans怎么样,这里有一篇文章如何用NB7进行设置:

http://netbeanside61.blogspot.com/2011/06/downloading-openjdk7-binary-for-mac-os.html http://netbeanside61.blogspot.com/2011/06/downloading-openjdk7-binary-for-mac-os.html

Maybe similar steps for Eclipse. Eclipse 可能有类似的步骤。

What worked for me on Lion was installing the JDK7_u17 from Oracle, then editing ~/.bash_profile to include: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home在 Lion 上对我有用的是从 Oracle 安装 JDK7_u17,然后编辑 ~/.bash_profile 以包括:export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home

Now, Use command现在,使用命令

Update 2020: 04 2020 年更新:04

To install Java7 with homebrew run:使用 homebrew 安装 Java7:

brew tap homebrew/cask-versions
brew cask install java7

Hope this help.希望这有帮助。

I needed to adapt @abe312's answer since there has been some changes with brew lately.我需要调整@abe312 的答案,因为最近brew发生了一些变化。

I installed zulu7 and setup JAVA_HOME by running:我通过运行安装了 zulu7 并设置了 JAVA_HOME:

brew install --cask homebrew/cask-versions/zulu7
echo "export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-7.jdk/Contents/Home" >> ~/.zshrc

I had to enter my password for installing zulu7.我必须输入密码才能安装 zulu7。 You may need to modify the last command if you are using a different shell.如果您使用不同的 shell,您可能需要修改最后一个命令。

The instructions by peter_budo worked perfectly. peter_budo 的说明效果很好。 I had to add the jars under /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib/ to my IntelliJ project libraries.我必须将/Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib/下的 jars 添加到我的 IntelliJ 项目库中。 Now it works like a charm.现在它就像一个魅力。 Note that I didn't need my IDE itself to run under 1.7;请注意,我不需要我的 IDE 本身在 1.7 下运行; rather, I only needed to be able to compile and run against 1.7.相反,我只需要能够针对 1.7 进行编译和运行。 I'll most likely continue to use Apple's JRE for running the IDE since it's probably more stable with respect to graphics routines (Swing, AWT).我很可能会继续使用 Apple 的 JRE 来运行 IDE,因为它在图形例程(Swing,AWT)方面可能更稳定。 Like the OP, I was really keen on testing out the new NIO2 API.和 OP 一样,我非常热衷于测试新的 NIO2 API。 Looking good so far.到目前为止看起来不错。 Thanks, Peter.谢谢,彼得。

I updated to Yosemite and Android Studio wouldn't clean my projects or Run them on virtual or real device because of the following error:由于以下错误,我更新到 Yosemite 和 Android Studio 不会清理我的项目或在虚拟或真实设备上运行它们:

Failed to complete Gradle execution.Cause:Supplied javaHome is not a valid folder. You supplied: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

After some research and trouble shooting, I found that the JDK file that was being pointed to at "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" wasn't there;经过一些研究和故障排除,我发现指向“/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home”的JDK文件不存在; all of "JavaVirtualMachines/1.6.0.jdk/Contents/Home" was missing from "/System/Library/Java". “/System/Library/Java”中缺少所有“JavaVirtualMachines/1.6.0.jdk/Contents/Home”。 So, I copied "JavaVirtualMachines/1.6.0.jdk/Contents/Home" over from "/Library/Java/" to "/System/Library/Java/" and cha ching.因此,我将“JavaVirtualMachines/1.6.0.jdk/Contents/Home”从“/Library/Java/”复制到“/System/Library/Java/”并进行了检查。 I was back in business.我回来做生意了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM