Change java compiler version - java

I get this error when running my Java application
Global : Unsupported major.minor version 52.0
When I checked my Java version, I found that it was different from the compiler version
java -version
# java version "1.7.0_65"
# OpenJDK Runtime Environment (IcedTea 2.5.1) (7u65-2.5.1-4ubuntu1~0.12.04.2)
# OpenJDK Server VM (build 24.65-b04, mixed mode)
javac -version
# javac 1.8.0_20
How do I downgrade the Java compiler?

Open a console. Type
sudo update-alternatives --config javac
After entering your root password you can enter the number of a javac with "java-7" in its name.
Edit: You could also update-alternatives --config java to choose a newer JVM that is compatible to Java 8 bytecode.

You don't need to downgrade the compiler; you just need to tell the compiler to produce classes that a Java 7 JVM will understand. Use the options -source 7 -target 7 when running javac.

I'm assuming you are using linux,
sudo update-alternatives --config javac
Then you can type your choice. If you get message like 'There is only one alternative...' then you should type to the terminal
sudo update-alternatives --install "/usr/bin/javac" "javac" "/(your java home)/bin/javac" 1

Related

Where is 'javac' in java-17-openjdk-amd64?

Win10 WSL 2 over an Ubuntu layer
Ubuntu: lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal\
Java: java --version
openjdk 17.0.1 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, Sharing)
I've successfully installed openjdk 17.0.1, but there is no 'javac' command in bin (such as exists in java-11-openjdk-amd64/bin). There is no javac counterpart in java-17-openjdk-amd64/bin.
I've googled, oogled, and bugled to no great gain. In my experience, this means I'm missing something simple.
Someone please guide me here. What gives? How can I get the 'javac' command in openjdk 17.0.1, or how do I use that SDK to create a class?
RHEL/RockyLinux/CentOs/AlmaLinux etc: javac is in "devel" package
dnf -y install java-17-openjdk
dnf -y install java-17-openjdk-devel
Then set default versions
alternatives --config java
alternatives --config javac
I'm unsure which directions I used to install Open JDK before, but I followed the following instructions to remove and re-install: https://www.linuxcapable.com/how-to-install-openjdk-17-on-ubuntu-20-04/
I now have 'javac' in java-17-openjdk-amd64/bin.
Whew.
This worked for me
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-17/bin/javac" 1

I can't find the javac for versions other than java17

I have installed multiple versions of java on my Linux mint mate 20.
I can find the multiple Java versions. But I can't find the multiple javac versions, javac is only present for Java 17.
I have installed all the Java version using
sudo apt-get install openjdk-x-jdk, where x is the the Java version.
This is the log of sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
* 2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:
This is the log of sudo update-alternatives --config javac
atul#mintMate:~$ sudo update-alternatives --config javac
There is only one alternative in link group javac (providing /usr/bin/javac): /usr/lib/jvm/java-17-openjdk-amd64/bin/javac
Nothing to configure.
atul#mintMate:~$
This is my terminal log
atul#mintMate:~$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, sharing)
atul#mintMate:~$ javac -version
javac 17.0.1
Is there a way to install or locate javac in the openjdk-amd64/bin/ folder. As I have already tried to look for them manually using the tree command.
Your Java 8 install is only for the Java Runtime Environment (JRE). Therefore, it won't have a Java compiler (javac). The compiler is part of the Java Development Kit (JDK).
Try installing Java 8 JDK: sudo apt-get install openjdk-8-jdk. It is likely that your Java 11 distribution is also a JRE-only install. If you need it, just install it the same way as I showed you for Java 8.

Changing java 7 to java 8 ubuntu openjdk not working

I have a project that runs on Jenkins using docker. At start the docker have java 7 installed by default which is necesary for the project to build. But after, the proyect build and is up, I need to install java 8 to run the e2e tests, selenium server needs java 8. So to install java 8 i use openjdk8 the following ways:
apt-get -y update
apt-get install -y openjdk-8-jdk
echo 'Changing Java version, it should be 8'
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
echo "path"
echo $JAVA_HOME
echo "version java"
java -version
The problem is that when this runs it still outputs the java 7. Below output from the code above.
Changing Java version, it should be 8
path
/usr/lib/jvm/java-8-openjdk-amd64
version java
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Any idea what could be happening?
you can do following:
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1
and then by using following you can set the config to use which version:
update-alternative --config java
update-alternative --config javac
Make sure that java8 home/bin is first in the path
update-java-alternatives --list | grep java-8
export JAVA_HOME= java 8 path here
export PATH=$JAVE_HOME/bin:$PATH

Set Java 1.6 default over 1.7 in Ubuntu Linux

Here is the output to some inspection that I did:
$ java -version
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
and
$ update-java-alternatives -l
java-1.6.0-openjdk-amd64 1061 /usr/lib/jvm/java-1.6.0-openjdk-amd64
and
$ ls /usr/lib/jvm/
default-java java-1.5.0-gcj-4.6 java-1.6.0-openjdk java-1.6.0-openjdk-amd64 java-6-openjdk java-6-openjdk-amd64 java-6-openjdk-common java-7-openjdk-amd64
With that in mind, how do I go about changing the default to 1.6 so that when I execute java -version, it tells me 1.6?
Thanks!
I am on Fedora but I hope that alternatives works the same way on Ubuntu. You need to run update-alternatives --config java (on Fedora just alternatives --config java) to see what options you have. Then type the number of the JDK you want to use and press enter. And that's it:-)
us below command to set the java.
sudo update-alternatives --config java
It will display the option to select you appropriate java version
Set default java:
mv /usr/bin/java /usr/bin/java_1.6
ln -s /usr/java/jdk1.7.0_55/bin/java /usr/bin/

Make cannot find tools.jar

Running Ubuntu 12.04
I have added to PATH: /home/jeffrey/jdk1.6.0_43/lib
I am attempting to build from source using Make:
make -j16
But encounter the error:
build/core/config.mk:268: *** Error: could not find jdk tools.jar, please install JDK6, which you can download from java.sun.com. Stop.
Can anyone explain to me why Make cannot find tools.jar?
EDIT
JAVA_HOME=/home/jeffrey/jdk1.6.0_43/bin/java
Determined by entering env command into the shell. Furthermore, this is set for all users in the etc/profile
Additionally:
jeffrey#jeffrey-Satellite-M645:~/WORKING_DIRECTORY$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
jeffrey#jeffrey-Satellite-M645:~/WORKING_DIRECTORY$
Setting the ANDROID_JAVA_HOME environment variable to JDK path, fixes this error.
I found the answer with this SO post.
Make sure that JAVA_HOME env variable is set to jdk-install-dir
Usually, this is caused by javac is not configured as a alternative for system. So there is no link of /usr/bin/javac for your javac.
Please setup the alternative for javac using the following command:
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.6.0_32/bin/javac" 1
note: the installed directory of javac for me is "/usr/lib/jvm/jdk1.6.0_32/", you can change it by your configuration.

Categories

Resources