4

I have a Ubuntu machine where I already have one JDK version which is installed. Next I have downloaded the second version into /usr/local.

Now, I want to run a program against the second JDK which is in /usr/local, i.e., I will copy a sample .java program in /usr/local/bin and then execute it.

But the problem is, even If I navigate to the /usr/local/bin and type $ java -version, I am getting the one which is installed in the first place. Is there any solution for this?

1

3 Answers 3

6

type sudo update-alternatives --config java

The select the version you want.

And I think this question should be moved to askubuntu.

Sign up to request clarification or add additional context in comments.

3 Comments

No, the second java is not installed , that is the problem here . I just have a zip file which contains bin and other things , which I have extracted to /usr/local/java
the the answer below should help
"Below" meaning "elsewhere" such as sometimes "above" depending on votes, edit history, and the user's preferences.
2

When you run java with no path, then your shell looks in your $PATH environment variable to find java. If you want a specific java, you need to either change your $PATH, or run it with a path, like:

/usr/local/bin/java

or if you really want to cd there first, you could run

./java

from /usr/local/bin

2 Comments

But how to compile, for eg: after cd to /usr/local/java/bin ,executing $javac sample.java will compile the extracted java or installed java version? because when I cd /usr/local/java/bin and if I type $java -version I still get the version of the one installed before
cd does not change where the shell looks for programs (except when . is in the $PATH). You need to use a full or relative path as in my answer, or change your PATH environment variable to contain the directory first where you want java or javac (or any other executable) to be found.
2

Here's an approach tested on Ubuntu 20.04 which shows adjusting the javac and java defaults:

$ 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

See Switch between multiple java versions on askubuntu for a canonical thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.