Ubuntu: OpenJDK 8 -无法定位包。

时间:2022-03-10 19:25:21

So I just installed Ubuntu, and this is my first time working in a Linux environment, so bear with my noobishness here.

所以我刚安装了Ubuntu,这是我第一次在Linux环境中工作,所以我要忍受我的noobi。

Anyway, I downloaded the Java 8 JDK directly from Oracle, but I understand there's also an OpenJDK for Java 8 as well. I have two questions.

无论如何,我直接从Oracle下载了Java 8 JDK,但是我知道Java 8也有一个OpenJDK。我有两个问题。

  1. What is the difference between the Oracle version and OpenJDK. Pros/Cons?

    Oracle版本和OpenJDK之间的区别是什么?优点/缺点?

  2. When I try and get OpenJDK 8, I can't. I'm pasting the terminal command and the error message I get here, hoping for advice. Again, very new to Linux, been on my first Linux distro ever for less than an hour here at this point.

    当我尝试使用OpenJDK 8时,我做不到。我正在粘贴终端命令和我到达这里的错误消息,希望得到建议。这是Linux的新版本,在我的第一个Linux发行版中,这是我在这一点上花了不到一个小时的时间。

    user@computer:~/Java$ sudo apt-get install openjdk-8-jdk
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    E: Unable to locate package openjdk-8-jdk
    

Any help would be appreciated. In the meantime, I'm just gonna use the Oracle package. Thanks.

如有任何帮助,我们将不胜感激。同时,我将使用Oracle包。谢谢。

Edit:

编辑:

So, now I have a new problem. I installed OpenJDK 7, it came with Maven and just installed before I realized I was getting a two-for-one deal. Now I have JDK 7, and Eclipse, and Maven, and they're all working, but I'm stuck on JDK 7 rather than 8.

现在我有了一个新问题。我安装了OpenJDK 7,它附带了Maven,并在我意识到我得到了一份两对一的协议之前安装了它。现在我有了JDK 7、Eclipse和Maven,它们都在工作,但是我被困在了JDK 7而不是8。

I saw that there's a link below, I intend to check it out in the morning, it's 2am and I'm tired right now. Any other advice for this new issue would be appreciated, otherwise I will definitely be checking out that link first thing in the morning.

我看到下面有个链接,我打算在早上查看一下,现在是凌晨2点,我现在很累。对于这个新问题的任何其他建议都将会得到赞赏,否则我肯定会在早上第一件事就是查看链接。

3 个解决方案

#1


83  

UPDATE: installation without root privileges below

更新:没有根权限的安装。


I advise you to not install packages manually on ubuntu system if there is already a (semi-official) repository able to solve your problem. Further, use Oracle JDK for development, just to avoid (very sporadic) compatibility issues (i've tried many years ago, it's surely better now).

如果已经有一个(半官方的)存储库能够解决您的问题,我建议您不要在ubuntu系统上手动安装包。此外,使用Oracle JDK进行开发,只是为了避免(非常分散的)兼容性问题(我多年前尝试过,现在肯定更好了)。

Add the webupd8 repo to your system:

将webupd8 repo添加到您的系统:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

Install your preferred version of jdk (versions from java-6 to java-9 available):

安装您首选的jdk版本(从java-6到java-9版本):

sudo apt-get install oracle-java8-installer

You can also install multiple version of jdk, mixing openjdk and oracle versions. Then you can use the command update-java-alternatives to switch between installed version:

您还可以安装多个版本的jdk,混合openjdk和oracle版本。然后,您可以使用命令update-java-替代品在已安装的版本之间切换:

# list available jdk
update-java-alternatives --list

# use jdk7
sudo update-java-alternatives --set java-7-oracle

# use jdk8
sudo update-java-alternatives --set java-8-oracle

Requirements

需求

If you get add-apt-repository: command not found be sure to have software-properties-common installed:

如果您获得了add-apt-repository:没有找到的命令一定要有软件属性—常见的安装:

sudo apt-get install software-properties-common

If you're using an older version Ubuntu:

如果你使用的是旧版本的Ubuntu:

sudo apt-get install python-software-properties

JDK installation without root privileges

没有根权限的JDK安装。

If you haven't administrator rights on your target machine your simplest bet is to use sdkman to install the zulu certified openjdk:

如果您的目标机器上没有管理员权限,那么最简单的方法就是使用sdkman安装zulu认证的openjdk:

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java

NOTE: sdkman allow to install also the official Oracle JDK, although it's not a the default option. View available versions with:

注意:sdkman允许安装官方的Oracle JDK,尽管它不是默认的选项。查看可用的版本:

sdk ls java

Install the chosen version with:

安装选择的版本:

sdk install java <version>

For example:

例如:

sdk install java 9.0.1-oracle

Glossary of commands

术语表的命令

  • sudo <command> [command_arguments]: execute a command with the superuser privilege.

    sudo <命令> [command_arguments]:使用超级用户特权执行命令。

  • add-apt-repository <PPA_id>: Ubuntu (just like every Debian derivatives and generally speaking every Linux distribution) has a main repository of packages that handle things like package dependencies and updating. In Ubuntu is possible to extend the main repository using a PPA (Personal Package Archive) that usually contains packages not available in the system (just like oracle jdk) or updated versions of available ones (example: LibreOffice 5 in LTS is available only through this PPA).

    扩展库 : Ubuntu(就像所有的Debian衍生工具,一般来说,每个Linux发行版)都有一个主要的包存储库,可以处理包依赖和更新之类的东西。在Ubuntu中,可以使用PPA(个人包存档)来扩展主存储库,它通常包含系统中不可用的包(就像oracle jdk一样)或更新版本的可用包(例如,LTS中的LibreOffice 5只能通过这个PPA提供)。

  • apt-get [install|update|upgrade|purge|...]: it's "the" command-line package handler used to manipulate the state of every repository on the system (installing / updating / upgrading can be viewed as an alteration of the repository current state).

    apt-get[安装| | |清洗升级更新|…]:它是用于操作系统上每个存储库状态的“命令行包处理程序”(安装/更新/升级可以看作是对存储库当前状态的更改)。

In our case: with the command sudo add-apt-repository ppa:webupd8team/java we inform the system that the next repository update must retrieve packages information also from webupd8 repo.

在我们的示例中:使用命令sudo add-ap -repository ppa:webupd8team/java,我们通知系统,下一个储存库更新必须从webupd8 repo检索包信息。

With sudo apt-get update we actually update the system repository (all this operations requires superuser privileges, so we prepend sudo to the commands).

通过sudo apt-get更新,我们实际上更新了系统存储库(所有这些操作都需要超级用户权限,所以我们将sudo预加到命令中)。

sudo apt-get install oracle-java8-installer

sudo apt-get安装oracle-java8-installer

  • update-java-alternatives (a specific java version of update-alternatives): in Ubuntu several packages provides the same functionality (browse the internet, compile mails, edit a text file or provides java/javac executables...). To allows the system to choose the user favourites tool given a specific task a mechanism using symlinks under /etc/alternatives/ is used. Try to update the jdk as indicated above (switch between java 7 and java 8) and view how change the output of this command:

    update-java-替代品(一个特定的java版本的更新替代品):在Ubuntu中,几个包提供了相同的功能(浏览internet、编译邮件、编辑文本文件或提供java/javac可执行文件)。为了使系统能够选择用户喜爱的工具,给定一个特定的任务,在/etc/ options /下使用符号链接的机制。尝试按照上面的指示更新jdk(在java 7和java 8之间切换),并查看如何更改此命令的输出:

    ls -l /etc/alternatives/java*

    ls - l /etc/alternatives / java

In our case: sudo update-java-alternatives --set java-8-oracle update symlinks under /etc/alternatives to point to java-8-oracle executables.

在我们的例子中:sudo update-java- options——设置java-8-oracle更新在/etc/ options下的符号链接,以指向java-8-oracle可执行文件。

Extras:

附加功能:

  • man <command>: start using man to read a really well written and detailed help on (almost) every shell command and its options (every command i mention in this little answer has a man page, try man update-java-alternatives).

    man <命令> :开始使用man来阅读非常好的书面和详细的帮助(几乎)每个shell命令和它的选项(我在这个小回答中提到的每个命令都有一个man页面,try man update-java- options)。

  • apt-cache search <search_key>: query the APT cache to search for a package related with the search_key provided (can be the package name or some word in package description).

    APT -cache搜索 :查询APT缓存,搜索与所提供的search_key相关的包(可以是包名称或包描述中的某个单词)。

  • apt-cache show <package>: provides APT information for a specific package (package version, installed or not, description).

    APT -cache显示 :为特定的包提供APT信息(包版本,安装或不安装,描述)。

#2


61  

As you can see I only have java 1.7 installed (on a Ubuntu 14.04 machine).

正如您所看到的,我只安装了java 1.7(在Ubuntu 14.04机器上)。

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64

To install Java 8, I did,

为了安装Java 8,我做到了,

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk

Afterwards, now I have java 7 and 8,

之后,我有了java 7和8,

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

BONUS ADDED (how to switch between different versions)

额外添加(如何在不同版本之间切换)

  • run the follwing command from the terminal:
  • 从终端运行以下命令:

sudo update-alternatives --config java

sudo update-alternatives——配置java

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1069      manual mode

Press enter to keep the current choice[*], or type selection number:

As you can see I'm running open jdk 8. To switch to to jdk 7, press 1 and hit the Enter key. Do the same for javac as well with, sudo update-alternatives --config javac.

可以看到,我正在运行open jdk 8。切换到jdk 7,按1并按回车键。对javac和sudo update-替代品——config javac进行同样的操作。

Check versions to confirm the change: java -version and javac -version.

检查版本以确认更改:java版本和javac版本。

#3


12  

I'm getting OpenJDK 8 from the official Debian repositories, rather than some random PPA or non-free Oracle binary. Here's how I did it:

我从官方的Debian存储库获得OpenJDK 8,而不是一些随机的PPA或非免费的Oracle二进制文件。我是这样做的:

sudo apt-get install debian-keyring debian-archive-keyring

sudo apt-get安装debian-keyring debian-archive-keyring。

Make /etc/apt/sources.list.d/debian-jessie-backports.list:

使/etc/apt/sources.list.d / debian-jessie-backports.list:

deb http://httpredir.debian.org/debian/ jessie-backports main

Make /etc/apt/preferences.d/debian-jessie-backports:

使/etc/apt/preferences.d / debian-jessie-backports:

Package: *
Pin: release o=Debian,a=jessie-backports
Pin-Priority: -200

Then finally do the install:

最后安装:

sudo apt-get update
sudo apt-get -t jessie-backports install openjdk-8-jdk

#1


83  

UPDATE: installation without root privileges below

更新:没有根权限的安装。


I advise you to not install packages manually on ubuntu system if there is already a (semi-official) repository able to solve your problem. Further, use Oracle JDK for development, just to avoid (very sporadic) compatibility issues (i've tried many years ago, it's surely better now).

如果已经有一个(半官方的)存储库能够解决您的问题,我建议您不要在ubuntu系统上手动安装包。此外,使用Oracle JDK进行开发,只是为了避免(非常分散的)兼容性问题(我多年前尝试过,现在肯定更好了)。

Add the webupd8 repo to your system:

将webupd8 repo添加到您的系统:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

Install your preferred version of jdk (versions from java-6 to java-9 available):

安装您首选的jdk版本(从java-6到java-9版本):

sudo apt-get install oracle-java8-installer

You can also install multiple version of jdk, mixing openjdk and oracle versions. Then you can use the command update-java-alternatives to switch between installed version:

您还可以安装多个版本的jdk,混合openjdk和oracle版本。然后,您可以使用命令update-java-替代品在已安装的版本之间切换:

# list available jdk
update-java-alternatives --list

# use jdk7
sudo update-java-alternatives --set java-7-oracle

# use jdk8
sudo update-java-alternatives --set java-8-oracle

Requirements

需求

If you get add-apt-repository: command not found be sure to have software-properties-common installed:

如果您获得了add-apt-repository:没有找到的命令一定要有软件属性—常见的安装:

sudo apt-get install software-properties-common

If you're using an older version Ubuntu:

如果你使用的是旧版本的Ubuntu:

sudo apt-get install python-software-properties

JDK installation without root privileges

没有根权限的JDK安装。

If you haven't administrator rights on your target machine your simplest bet is to use sdkman to install the zulu certified openjdk:

如果您的目标机器上没有管理员权限,那么最简单的方法就是使用sdkman安装zulu认证的openjdk:

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java

NOTE: sdkman allow to install also the official Oracle JDK, although it's not a the default option. View available versions with:

注意:sdkman允许安装官方的Oracle JDK,尽管它不是默认的选项。查看可用的版本:

sdk ls java

Install the chosen version with:

安装选择的版本:

sdk install java <version>

For example:

例如:

sdk install java 9.0.1-oracle

Glossary of commands

术语表的命令

  • sudo <command> [command_arguments]: execute a command with the superuser privilege.

    sudo <命令> [command_arguments]:使用超级用户特权执行命令。

  • add-apt-repository <PPA_id>: Ubuntu (just like every Debian derivatives and generally speaking every Linux distribution) has a main repository of packages that handle things like package dependencies and updating. In Ubuntu is possible to extend the main repository using a PPA (Personal Package Archive) that usually contains packages not available in the system (just like oracle jdk) or updated versions of available ones (example: LibreOffice 5 in LTS is available only through this PPA).

    扩展库 : Ubuntu(就像所有的Debian衍生工具,一般来说,每个Linux发行版)都有一个主要的包存储库,可以处理包依赖和更新之类的东西。在Ubuntu中,可以使用PPA(个人包存档)来扩展主存储库,它通常包含系统中不可用的包(就像oracle jdk一样)或更新版本的可用包(例如,LTS中的LibreOffice 5只能通过这个PPA提供)。

  • apt-get [install|update|upgrade|purge|...]: it's "the" command-line package handler used to manipulate the state of every repository on the system (installing / updating / upgrading can be viewed as an alteration of the repository current state).

    apt-get[安装| | |清洗升级更新|…]:它是用于操作系统上每个存储库状态的“命令行包处理程序”(安装/更新/升级可以看作是对存储库当前状态的更改)。

In our case: with the command sudo add-apt-repository ppa:webupd8team/java we inform the system that the next repository update must retrieve packages information also from webupd8 repo.

在我们的示例中:使用命令sudo add-ap -repository ppa:webupd8team/java,我们通知系统,下一个储存库更新必须从webupd8 repo检索包信息。

With sudo apt-get update we actually update the system repository (all this operations requires superuser privileges, so we prepend sudo to the commands).

通过sudo apt-get更新,我们实际上更新了系统存储库(所有这些操作都需要超级用户权限,所以我们将sudo预加到命令中)。

sudo apt-get install oracle-java8-installer

sudo apt-get安装oracle-java8-installer

  • update-java-alternatives (a specific java version of update-alternatives): in Ubuntu several packages provides the same functionality (browse the internet, compile mails, edit a text file or provides java/javac executables...). To allows the system to choose the user favourites tool given a specific task a mechanism using symlinks under /etc/alternatives/ is used. Try to update the jdk as indicated above (switch between java 7 and java 8) and view how change the output of this command:

    update-java-替代品(一个特定的java版本的更新替代品):在Ubuntu中,几个包提供了相同的功能(浏览internet、编译邮件、编辑文本文件或提供java/javac可执行文件)。为了使系统能够选择用户喜爱的工具,给定一个特定的任务,在/etc/ options /下使用符号链接的机制。尝试按照上面的指示更新jdk(在java 7和java 8之间切换),并查看如何更改此命令的输出:

    ls -l /etc/alternatives/java*

    ls - l /etc/alternatives / java

In our case: sudo update-java-alternatives --set java-8-oracle update symlinks under /etc/alternatives to point to java-8-oracle executables.

在我们的例子中:sudo update-java- options——设置java-8-oracle更新在/etc/ options下的符号链接,以指向java-8-oracle可执行文件。

Extras:

附加功能:

  • man <command>: start using man to read a really well written and detailed help on (almost) every shell command and its options (every command i mention in this little answer has a man page, try man update-java-alternatives).

    man <命令> :开始使用man来阅读非常好的书面和详细的帮助(几乎)每个shell命令和它的选项(我在这个小回答中提到的每个命令都有一个man页面,try man update-java- options)。

  • apt-cache search <search_key>: query the APT cache to search for a package related with the search_key provided (can be the package name or some word in package description).

    APT -cache搜索 :查询APT缓存,搜索与所提供的search_key相关的包(可以是包名称或包描述中的某个单词)。

  • apt-cache show <package>: provides APT information for a specific package (package version, installed or not, description).

    APT -cache显示 :为特定的包提供APT信息(包版本,安装或不安装,描述)。

#2


61  

As you can see I only have java 1.7 installed (on a Ubuntu 14.04 machine).

正如您所看到的,我只安装了java 1.7(在Ubuntu 14.04机器上)。

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64

To install Java 8, I did,

为了安装Java 8,我做到了,

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk

Afterwards, now I have java 7 and 8,

之后,我有了java 7和8,

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

BONUS ADDED (how to switch between different versions)

额外添加(如何在不同版本之间切换)

  • run the follwing command from the terminal:
  • 从终端运行以下命令:

sudo update-alternatives --config java

sudo update-alternatives——配置java

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1069      manual mode

Press enter to keep the current choice[*], or type selection number:

As you can see I'm running open jdk 8. To switch to to jdk 7, press 1 and hit the Enter key. Do the same for javac as well with, sudo update-alternatives --config javac.

可以看到,我正在运行open jdk 8。切换到jdk 7,按1并按回车键。对javac和sudo update-替代品——config javac进行同样的操作。

Check versions to confirm the change: java -version and javac -version.

检查版本以确认更改:java版本和javac版本。

#3


12  

I'm getting OpenJDK 8 from the official Debian repositories, rather than some random PPA or non-free Oracle binary. Here's how I did it:

我从官方的Debian存储库获得OpenJDK 8,而不是一些随机的PPA或非免费的Oracle二进制文件。我是这样做的:

sudo apt-get install debian-keyring debian-archive-keyring

sudo apt-get安装debian-keyring debian-archive-keyring。

Make /etc/apt/sources.list.d/debian-jessie-backports.list:

使/etc/apt/sources.list.d / debian-jessie-backports.list:

deb http://httpredir.debian.org/debian/ jessie-backports main

Make /etc/apt/preferences.d/debian-jessie-backports:

使/etc/apt/preferences.d / debian-jessie-backports:

Package: *
Pin: release o=Debian,a=jessie-backports
Pin-Priority: -200

Then finally do the install:

最后安装:

sudo apt-get update
sudo apt-get -t jessie-backports install openjdk-8-jdk