Resolving version conflict between java and javac on Ubuntu
Asked Answered
P

5

23

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:

$ 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?

Pollination answered 14/5, 2012 at 14:15 Comment(2)
you may prefer to ask this question on askubuntu.com Anyways, check what java packages are installed and if the jre v1.7 is also installed you may need to configure what version to use with "alternatives"Ambages
Alternatively, try compiling with -target 1.6 or -source 1.6.Teenybopper
N
9

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:

sudo ln -f -s [put location here]/bin/* /usr/bin
Nashner answered 14/5, 2012 at 14:36 Comment(4)
I am having the same issue, but when I try this I get a new, different error: "too many levels of symbolic links"Sym
-1, please do not attempt this answer. I think it should be unselected as the accepted answer. This will most likely cause you machine to irreversibly messed up by creating circular symlinks all over your /usr/bin directory that cannot be deleted!Sym
@Sym This answer was directed at a person whose javac was not in /usr/bin. If [put location here]/bin turned out to be /usr/bin then the command becomes something completely ridiculous which you should have known - if you had tried to understand this solution instead of applying it blindlyDelk
I suppose that is true. I didn't mean to offend, and I think the answer is still useful. But maybe my public stupidity will prevent someone else from making the same mistake.Sym
W
30

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).

  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

Wares answered 28/5, 2013 at 7:58 Comment(1)
This option worked for me on Ubuntu 18.04Jurado
A
21

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

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
Aggy answered 17/6, 2015 at 6:58 Comment(0)
N
9

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:

sudo ln -f -s [put location here]/bin/* /usr/bin
Nashner answered 14/5, 2012 at 14:36 Comment(4)
I am having the same issue, but when I try this I get a new, different error: "too many levels of symbolic links"Sym
-1, please do not attempt this answer. I think it should be unselected as the accepted answer. This will most likely cause you machine to irreversibly messed up by creating circular symlinks all over your /usr/bin directory that cannot be deleted!Sym
@Sym This answer was directed at a person whose javac was not in /usr/bin. If [put location here]/bin turned out to be /usr/bin then the command becomes something completely ridiculous which you should have known - if you had tried to understand this solution instead of applying it blindlyDelk
I suppose that is true. I didn't mean to offend, and I think the answer is still useful. But maybe my public stupidity will prevent someone else from making the same mistake.Sym
T
3

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

Trutko answered 14/5, 2012 at 14:27 Comment(0)
S
2

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

Steps 1: Add the Java and Javac alternatives :

Command to add 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

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

Steps 2: Installed the Java and Javac alternatives :

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.

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.

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

which javac and which java you will able to see screen like below :

enter image description here

Stopple answered 25/11, 2019 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.