我有一台ubuntu机器,我安装了一个jdk版本。 接下来我下载了第二个版本/ usr / local
现在,我想对/ usr / local中的第二个jdk运行一个程序,即 我将在/ usr / local / bin中复制一个示例.java程序,然后执行。
但问题是,即使我导航到/ usr / local / bin并输入$ java -version 我正在获得首先安装的那个。对此有何解决方案?
答案 0 :(得分:2)
输入sudo update-alternatives --config java
选择所需的版本。
我认为这个问题应该转移到askubuntu
答案 1 :(得分:1)
当您运行java
时没有路径时,您的shell会在$PATH
环境变量中查找java
。
如果您需要特定的java
,则需要更改$PATH
,或使用路径运行它,例如:
/usr/local/bin/java
或者如果您真的想先cd
,那么您可以运行
./java
来自/usr/local/bin
答案 2 :(得分:0)
以下是在 Ubuntu 20.04 上测试的方法,显示了如何调整 javac
和 java
默认值:
$ sudo apt install openjdk-14-jdk
$ sudo apt install openjdk-8-jdk
$ java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 14.0.2+12-Ubuntu-120.04, mixed mode, sharing)
$ javac -version
javac 14.0.2
$ update-java-alternatives --list
java-1.14.0-openjdk-amd64 1411 /usr/lib/jvm/java-1.14.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 auto mode
1 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode
$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 auto mode
1 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode
$ java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)
$ javac -version
javac 1.8.0_275
有关规范线程,请参阅 askubuntu 上的 Switch between multiple java versions。