何时以及为何在 Linux 中安装 jdk 时运行替代方案 --install java jar javac javaws
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32746419/
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
When and Why run alternatives --install java jar javac javaws on installing jdk in linux
提问by Charu Khurana
To install java in linux (I used CentOS, RHEL is same too), I used this command
在linux下安装java(我用的是CentOS,RHEL也是一样),我用了这个命令
rpm -Uvh /path/to/binary/jdk-7u55-linux-x64.rpm
rpm -Uvh /path/to/binary/jdk-7u55-linux-x64.rpm
and verified java
和验证的java
java -version
java -version
Looking at a tutorial, it says to run following 4 commands, not sure why
查看教程,它说要运行以下 4 个命令,不知道为什么
## java ##
alternatives --install /usr/bin/java java /usr/java/latest/jre/bin/java 200000
## javaws ##
alternatives --install /usr/bin/javaws javaws /usr/java/latest/jre/bin/javaws 200000
## Install javac only
alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
## jar ##
alternatives --install /usr/bin/jar jar /usr/java/latest/bin/jar 200000
I know if there are multiple versions of java installed, you can select version to use from
我知道如果安装了多个版本的java,您可以从中选择要使用的版本
alternatives --config java
then why to run alternative --install
separately for each executable.
那么为什么要alternative --install
为每个可执行文件单独运行。
I've seen thisquestion but doesn't get my answer
我看过 这个问题,但没有得到我的答案
采纳答案by James Jithin
When you install JDK on Linux, it it gets installed depends on the type of package, version and distribution. You can refer to the following links for information about the installation location on linux:
在 Linux 上安装 JDK 时,它的安装取决于包的类型、版本和发行版。linux上的安装位置可以参考以下链接:
JDK Installation for Linux Platforms - Version 8
JDK Installation for Linux Platforms - Version 7
Once you install JDK, the bin
folder containing tools might not get added to the environment variable PATH
. Commands typed on the terminal needs to be from the locations specified in the PATH
variable. In cases when JDK\bin
does not get added, the user would need to configure it manually as mentioned in Installing the JDK Software
安装 JDK 后,bin
包含工具的文件夹可能不会添加到环境变量中PATH
。在终端上键入的命令需要来自PATH
变量中指定的位置。如果JDK\bin
没有添加,用户需要手动配置它,如安装 JDK 软件中所述
alternatives
command is being used to create a symbolic link. Here, it is being directed to use the command to add the tools like javac
, javaw
to /usr/bin
which exists in the PATH
variable by default.
alternatives
命令用于创建符号链接。在这里,它被直接使用命令来添加工具,如javac
,javaw
以/usr/bin
中存在的PATH
默认变量。
If(you could execute java -version
outside of JDK/bin
&¬ by specifying the complete path &&if the version and bundle prints to be as that of package you installed ){you need not run the alternatives
command.}
如果(您可以java -version
在JDK/bin
&&之外执行,而不是通过指定完整路径&&如果版本和捆绑包打印为您安装的包的版本){您不需要运行alternatives
命令。}
回答by djs
java
, javaws
, javac
, and jar
are all different executables used by the JDK. When you run alternatives --config java
you are only configuring which version of the java
executable you wish to run. However, the JDK includes, for example, the javac
compiler. You need to configure which version of the compiler you wish to use as well.
java
、javaws
、javac
和jar
都是 JDK 使用的不同可执行文件。当您运行时,alternatives --config java
您只是在配置java
您希望运行的可执行文件的哪个版本。但是,JDK 包括例如javac
编译器。您还需要配置要使用的编译器版本。
The alternativescommand is, in a nutshell, used to maintain a lookup for symbolic (or sym) links. Before you can choose which version of java
you want to run with the --configure
option, you must first register the actual path to the executable with the --install
option. alternatives --install
is not installing any software. It is merely registering some paths and aliases with the alternatives
framework. (Note: alternatives
is not using the alias
command. I mean "aliases" in the traditional, literal sense.)
的替代命令是,概括地说,用于维护符号(或符号)的链接的查找。在您选择java
要使用该--configure
选项运行的版本之前,您必须首先使用该选项注册可执行文件的实际路径--install
。alternatives --install
没有安装任何软件。它只是向alternatives
框架注册一些路径和别名。 (注意:alternatives
不是使用alias
命令。我的意思是传统的字面意义上的“别名”。)
You should also understand what the rpm
command does. Really, it is only dropping down a set of binaries into a particular directory. This directory may be long and tedious to explicitly specify: /some/path/to/lib/jvm/java-1.x.x-etc-etc-x86/jre/bin/java
. You don't want to specify this every time you want to run java
. Instead, we set up some sym links.
您还应该了解该rpm
命令的作用。实际上,它只是将一组二进制文件放到特定目录中。明确指定此目录可能很长很乏味:/some/path/to/lib/jvm/java-1.x.x-etc-etc-x86/jre/bin/java
. 您不想在每次运行时都指定此项java
。相反,我们设置了一些符号链接。
You might also want to read up on how the PATH
works in linux.
It may become clearer if you try tracing through the setup for your machine. Here is what I ran:
如果您尝试跟踪机器的设置,它可能会变得更清楚。这是我跑的:
> which java
/usr/bin/java
> ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Aug 14 2014 /usr/bin/java -> /etc/alternatives/java
> ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 73 Aug 14 2014 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65-2.5.1.2.fc19.x86_64/jre/bin/java
回答by Rostislav Matl
These "alternatives" scripts and similar tools just symlink binaries of desired JDK version to the directories they are expected to be. It's done for all commands that should be available on your command line.
这些“替代”脚本和类似工具只是将所需 JDK 版本的二进制文件符号链接到它们预期的目录。它已为您的命令行上应该可用的所有命令完成。
Definition of "all" depends on your distro.
“全部”的定义取决于您的发行版。
回答by jmojico
To get a different version of java JRE/JDK as default in your OS:
要在您的操作系统中默认使用不同版本的 java JRE/JDK:
sudo alternatives --install /usr/bin/java java [path_to_java_bin] 1
Example in my case was:
我的例子是:
sudo alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_80/bin/java 1
Then run a config to select the alternative java version:
然后运行配置以选择替代的 Java 版本:
sudo /usr/sbin/alternatives --config java
Choose 1,2 or the option you desire
选择 1,2 或您想要的选项
Then check the changes by doing:
然后通过执行以下操作检查更改:
java -version
回答by Dnyaneshwar Harer
For Ubuntu use update-alternatives
对于 Ubuntu 使用 update-alternatives
回答by alfred
Maybe, just the guide is outdated.
也许,只是指南已经过时了。
After you install the rpm, you can check:
安装 rpm 后,您可以检查:
alternative --display java
替代--显示java
to see if you can see the new version is controlled by alternative, if not you need to "--install" them; otherwise, I think you can just skip "--install" part.
看看你是否可以看到新版本是由替代控制的,如果不是你需要“--install”它们;否则,我认为您可以跳过“--install”部分。