0

I usually use eclipse , but I said "lets do some fantasy" to myself and things happened. I tried to use terminal command javac at first. I got

 The program 'javac' can be found in the following packages:

* default-jdk
* ecj
* gcj-5-jdk
* openjdk-8-jdk-headless
* gcj-4.8-jdk
* gcj-4.9-jdk
* openjdk-9-jdk-headless

Try: sudo apt install (selected package)

I already installed jre8 and jdk8 manually from oracle's website (standard edition) and I'm making programs with eclipse already. Then I typed java -version and the answer was

openjdk version "9-internal"
OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src)
OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode)

which is not what i installed manually. Then I used locate -br ^javac$ and got two locations (not a surprise). So:

  1. I can use javac only with the path to where I installed jdk manually, but I wanna use commands without path, just directly . Is that possible (if yes then how ?
  2. It seems like I have two java versions. Does that cause any problem? If it does, tell me how to fix please (I prefer eliminating openjdk9)
Zanna
  • 70,465
buraky
  • 121

1 Answers1

1

You can choose your default java by running update-alternatives:

$ sudo update-alternatives --config java

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

Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-9-oracle/bin/java              1091      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
* 3            /usr/lib/jvm/java-9-oracle/bin/java              1091      manual mode
  4            /usr/lib/jvm/jdk1.8.0_91                         100       manual mode
  5            /usr/lib/jvm/jdk1.8.0_91/bin/java                100       manual mode

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

Now to change your version simple type a number and hit Enter or to keep the default, just hit Enter. Do the same for the compiler :

sudo update-alternatives --config javac

And make your choice. Now one can also use a better command update-java-alternatives as recommended by Chai.

Now assuming you have these java packages installed:

java-9-oracle
java-8-openjdk-amd64
jdk1.8.0_91

And I want the java-9-oracle version, then I will simply run:

sudo update-java-alternatives -s <java_name>

sudo update-java-alternatives -s java-9-oracle

This would create all the needed links to my desired java version.

Now in the light that you placed java in a different location we will use update-alternatives --install to let it know where our java is.

sudo update-alternatives --install /usr/bin/java java /usr/share/java/jdk1.8.0_121/bin/java 1

And for the compiler javac the same

sudo update-alternatives --install /usr/bin/javac javac /usr/share/java/jdk1.8.0_121/bin/javac 1

Source: man update-java-alternatives

George Udosen
  • 36,677
  • update-java-alternatives is better, as it changes all the binaries (javac, javap, jar, etc). – Chai T. Rex Mar 15 '17 at 00:38
  • Thanks a lot for your answers ! Unfortunately my problem is not solved . When i downloaded java manually , intructions were telling me to unzip .tar.gz file in any directory i want to install java . so my directory is "/usr/share/java/jdk1.8.0_121" . but when i try your codes , im receiving an error as "update-java-alternatives: directory does not exist: /usr/lib/jvm/jdk1.8.0_121" and with other code , im only viewing folders under "/usr/lib/jvm/". what shoul i do now? @Chai – buraky Mar 16 '17 at 13:20
  • Ok let me update the answer so you can add it with update-alternatives --install – George Udosen Mar 16 '17 at 13:36
  • @Letitbe please see updated answer... – George Udosen Mar 16 '17 at 13:46