在 Ubuntu 上解决 java 和 javac 之间的版本冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10585091/
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-10-31 01:40:49  来源:igfitidea点击:

Resolving version conflict between java and javac on Ubuntu

ubuntujavajavacopenjdkunsupported-class-version

提问by xralf

I have a problem with my compiled Java application on Ubuntu. It throws UnsupportedClassVersionError. I am compiling with a higher JDK version than the one that is configured in my PATH to run Java:

我在 Ubuntu 上编译的 Java 应用程序有问题。它抛出UnsupportedClassVersionError. 我正在使用比我的 PATH 中配置的更高版本的 JDK 版本进行编译以运行 Java:

$ javac -version
javac 1.7.0_147

$ java -version
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2)
OpenJDK Client VM (build 20.0-b11, mixed mode, sharing)

How can I resolve this?

我该如何解决这个问题?

采纳答案by thejartender

Run either of the following to locate where the location is of that

运行以下任一命令以定位该位置的位置

javac 1.7.0_147

being used as follows:

用法如下:

whereis javac

or

或者

 locate javac

or

或者

find javac

Then once you found the directory, symbolically and forcefully link the bin contents to /usr/bin:

然后,一旦找到目录,就象征性地将 bin 内容强制链接到 /usr/bin:

sudo ln -f -s [put location here]/bin/* /usr/bin

回答by Ruju

Type following command in terminal :

在终端中输入以下命令:

  sudo update-alternatives --config java

It will ask you for:

它会问你:

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

替代 java 有 2 个选择(提供 /usr/bin/java)。

  Selection      Path                                            Priority   Status

    0           /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode

    1           /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode

    2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java  1051      manual mode


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

I enter 2 as I want to use java 1.7.0_21

我输入 2 因为我想使用 java 1.7.0_21

回答by Tony

You will also need to do:

您还需要执行以下操作:

sudo update-alternatives --config javac

Select the desired java compiler from the list provided. For example on Centos 5

从提供的列表中选择所需的 Java 编译器。例如在 Centos 5

There are 2 programs which provide 'javac'.
Selection    Command
-----------------------------------------------
1           /usr/lib/jvm/java-1.6.0-openjdk/bin/javac
*+ 2        /usr/lib/jvm/java-1.7.0-openjdk/bin/javac

回答by apesa

This is what you want Java and Ubuntu. I have the same config basically, but I am switching back and forth from Glassfish and Java7. Take a look and try setting up your Java env in a similar fashion

这就是你想要的Java 和 Ubuntu。我基本上有相同的配置,但我在 Glassfish 和 Java7 之间来回切换。看看并尝试以类似的方式设置您的 Java 环境

回答by Laxman G

I have added and installed alternatives for Java and Javac with below steps it work for me :

我已经添加并安装了 Java 和 Javac 的替代品,以下步骤对我有用:

Steps 1: Add the Java and Javac alternatives:

步骤 1添加 Java 和 Javac 替代品

Command to add Javac:

添加Javac的命令:

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/amazon-jdk-11/bin/java" 1 

i.e : "/opt/amazon-jdk-11/bin/javac" this the new path of java

即:“/opt/amazon-jdk-11/bin/javac”这是java的新路径

sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/amazon-jdk-11/bin/javac" 1

i.e : /opt/amazon-jdk-11/bin/javac is the new path of javac

即:/opt/amazon-jdk-11/bin/javac 是javac的新路径

Steps 2: Installed the Java and Javac alternatives:

步骤 2安装 Java 和 Javac 替代品

sudo update-alternatives --config javac
sudo update-alternatives --config javac

Once you enter above command you will able to see screen like below you can select your number that you want to configure java.

输入上述命令后,您将能够看到如下屏幕,您可以选择要配置 java 的号码。

enter image description here

在此处输入图片说明

I have selected " 1"int the list as I wanted to installed "/opt/amazon-jdk-11/bin/java" as my javac.

我在列表中选择了“ 1”,因为我想安装“/opt/amazon-jdk-11/bin/java”作为我的 javac。

Verify your Java alternative installation is successful or not with below commands:

使用以下命令验证您的Java 替代安装是否成功

which javacand which javayou will able to see screen like below :

which javacwhich java你将能够看到屏幕象下面这样:

enter image description here

在此处输入图片说明