Linux set which java

How to set default Java version?

I followed all the instructions stated at this question, but am encountering some problems with the last part of it. I actually have version 6.22 of java and would like to update to version 6.30. So after moving the extracted directory java-6-oracle into /usr/lib/jvm I do not know what to do, since the script that is pointed out in the answer above updates from java 5 to java 6. For sake of clearness here is output if I do an ls in dir /usr/lib/jvm :

$ ls -l /usr/lib/jvm total 8 lrwxrwxrwx 1 root root 14 2011-07-12 15:18 default-java -> java-6-openjdk lrwxrwxrwx 1 root root 14 2011-07-12 12:19 java-1.6.0-openjdk -> java-6-openjdk drwxr-xr-x 10 root root 4096 2012-04-12 12:06 java-6.31-oracle drwxr-xr-x 7 root root 4096 2012-02-24 14:43 java-6-openjdk 

What should I do now? ADDED PART Under the suggestion of @fossfreedom I ran the script anyway and actually it updated the java version. In fact if I run command java -version output will be the following:

$ java -version java version "1.6.0_31" Java(TM) SE Runtime Environment (build 1.6.0_31-b04) Java HotSpot(TM) Server VM (build 20.6-b01, mixed mode) 

There is still a problem, if Irun javac -version it gives me the old version installed:

$ javac -version javac 1.6.0_22 
Java Version 1.6.0_22 from Sun Microsystems Inc. 

What’s going wrong? It seems that Java Runtime Environment has updated, but Java Compiler and Java plugin for browser have not. How can I update them? OTHER ADDED PART sudo update-alternatives —config java will return following output

$ 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-6.31-oracle/bin/java 1062 auto mode 1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode 2 /usr/lib/jvm/java-6.31-oracle/bin/java 1062 manual mode Press enter to keep the current choice[*], or type selection number: 

these makes sense with the fact that JRE is correctly updated to version 6.31, issues are on Java Compiler and Java browser plugin. Any ideas?

Источник

«which java» in CentOS prints wrong java path

I am not sure why «which java» and «whereis java» paths are not correct. I tried to edit ~/.bash_profile and /etc/environment but did not help. The desired path is what is seen in «echo $JAVA_HOME» below but the same is not reflected in «which java» Below is what I get in CentOS 6.4: which java /usr/bin/java java -version java version «1.7.0_45» JAVA(TM) SE Runtime Environment (build 1.7.0_45-b18) JAVA HotSpot (TM) 64-bit Server VM (build 24.45-b08, mixed mode) whereis java java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java echo $JAVA_HOME /usr/java/jdk1.7.0_45/jre => desired shows correct when echo $JAVA_HOME

3 Answers 3

Run alternatives —config java to pick the Java version you want to use as default. It will print out a list of installed Javas to choose from.

which java , however, will always print out /usr/bin/java . This doesn’t mean it’s set wrong! Observe:

$ ls -l `which java` lrwxrwxrwx 1 root root 22 Oct 19 11:49 /usr/bin/java -> /etc/alternatives/java $ ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 35 Oct 19 11:49 /etc/alternatives/java -> /usr/lib/jvm/jre-1.5.0-gcj/bin/java 

If you use alternatives to change the path to IcedTea, ls -l /etc/alternatives/java will reflect that.

Источник

Switch Java Version with update-alternatives

In Linux, like Debian or Ubuntu system, want to install different version of java and can swith system java version easily, the easiest solution is use update-alternatives .

update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.

How update-alternatives works

It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. For example, many systems have several text editors installed at once. This gives choice to the users of a system, allowing each to use a different editor, if desired, but makes it difficult for a program to make a good choice for an editor to invoke if the user has not specified a particular preference.

Debian’s alternatives system aims to solve this problem. A generic name in the filesystem is shared by all files providing interchangeable functionality. The alternatives system and the system administrator together determine which actual file is referenced by this generic name. For example, if the text editors ed(1) and nvi(1) are both installed on the system, the alternatives system will cause the generic name /usr/bin/editor to refer to /usr/bin/nvi by default. The system administrator can override this and cause it to refer to /usr/bin/ed instead, and the alternatives system will not alter this setting until explicitly requested to do so.

The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory, which in turn is a symbolic link to the actual file referenced. This is done so that the system administrator’s changes can be confined within the /usr/local/etc directory.

Where is java

In Debian/Ubunut, java is symbol link in /bin/java which point to /etc/alternatives/java , which is another symbol link, it point to the current java path.

# which java /bin/java # ls -l /bin/java lrwxrwxrwx 1 root root 22 Mar 25 20:53 /bin/java -> /etc/alternatives/java # ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 46 Apr 7 09:07 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 

List installed java versions

Use update-alternatives —list java to list all installed java paths. Example:

# update-alternatives --list java /usr/lib/jvm/java-11-openjdk-arm64/bin/java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 

Switch java version with update-alternative

Use update-alternatives —config java to update java version in interactive mode. Example:

# 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-11-openjdk-arm64/bin/java 1111 auto mode * 1 /usr/lib/jvm/java-11-openjdk-arm64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java to provide /usr/bin/java (java) in manual mode # java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) 

When using the —config option, update-alternatives will list all of the choices for the link group of which given name is the master alternative name. The current choice is marked with a * . You will then be prompted for your choice regarding this link group. Depending on the choice made, the link group might no longer be in auto mode. You will need to use the —auto option in order to return to the automatic mode (or you can rerun —config and select the entry marked as automatic).

If you want to configure non-interactively you can use the —set option instead. For example, in script you donot want user input. You can use update-alternatives —set java to update java version directly. Example:

# update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java update-alternatives: using /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java to provide /usr/bin/java (java) in manual mode root@9b816ba2e3cb:/project# java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) 

Use update-java-alternatives to update java version

Alternative, a dedicate command update-java-alternatives can also be used to switch java version.

update-java-alternatives updates all alternatives belonging to one runtime or development kit for the Java language. A package does provide these information of it’s alternatives in /usr/lib/jvm/..jinfo .

Use -l to list available java version example:

# update-java-alternatives -l java-1.11.0-openjdk-arm64 1111 /usr/lib/jvm/java-1.11.0-openjdk-arm64 java-1.8.0-openjdk-arm64 1081 /usr/lib/jvm/java-1.8.0-openjdk-arm64 

Use -s to set java version to Java 11 example:

# update-java-alternatives -s java-1.11.0-openjdk-arm64 update-alternatives: error: no alternatives for mozilla-javaplugin.so # java -version openjdk version "11.0.14" 2022-01-18 OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode) 

Get JAVA_HOME

To get JAVA_HOME , use java -XshowSettings:properties -version to print all current java property settings and look for java.home . Example:

# java -XshowSettings:properties -version Property settings: awt.toolkit = sun.awt.X11.XToolkit file.encoding = UTF-8 file.separator = / java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment java.awt.printerjob = sun.print.PSPrinterJob java.class.path = java.class.version = 55.0 java.home = /usr/lib/jvm/java-11-openjdk-arm64 java.io.tmpdir = /tmp java.library.path = /usr/java/packages/lib /usr/lib/aarch64-linux-gnu/jni /lib/aarch64-linux-gnu /usr/lib/aarch64-linux-gnu /usr/lib/jni /lib /usr/lib java.runtime.name = OpenJDK Runtime Environment java.runtime.version = 11.0.14+9-Ubuntu-0ubuntu2.20.04 java.specification.name = Java Platform API Specification java.specification.vendor = Oracle Corporation java.specification.version = 11 java.vendor = Ubuntu java.vendor.url = https://ubuntu.com/ java.vendor.url.bug = https://bugs.launchpad.net/ubuntu/+source/openjdk-lts java.version = 11.0.14 java.version.date = 2022-01-18 java.vm.compressedOopsMode = 32-bit java.vm.info = mixed mode java.vm.name = OpenJDK 64-Bit Server VM java.vm.specification.name = Java Virtual Machine Specification java.vm.specification.vendor = Oracle Corporation java.vm.specification.version = 11 java.vm.vendor = Ubuntu java.vm.version = 11.0.14+9-Ubuntu-0ubuntu2.20.04 jdk.debug = release line.separator = \n os.arch = aarch64 os.name = Linux os.version = 5.10.0-12-arm64 path.separator = : sun.arch.data.model = 64 sun.boot.library.path = /usr/lib/jvm/java-11-openjdk-arm64/lib sun.cpu.endian = little sun.cpu.isalist = sun.io.unicode.encoding = UnicodeLittle sun.java.launcher = SUN_STANDARD sun.jnu.encoding = UTF-8 sun.management.compiler = HotSpot 64-Bit Tiered Compilers sun.os.patch.level = unknown user.country = US user.dir = /project user.home = /root user.language = en user.name = root user.timezone = openjdk version "11.0.14" 2022-01-18 OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode) 

To get JAVA_HOME , check the java.home property. Example:

# java -XshowSettings:properties -version 2>&1 |grep java.home java.home = /usr/lib/jvm/java-11-openjdk-arm64 

Use grep and awk to extract value of java.home and set to JAVA_HOME . Example:

# java -XshowSettings:properties -version 2>&1 |grep java.home | awk -F= '' /usr/lib/jvm/java-11-openjdk-arm64 # JAVA_HOME=$(java -XshowSettings:properties -version 2>&1 |grep java.home | awk -F= '') 

Install different version of java

First list available java version in apt . Example:

# apt-cache search openjdk | grep '[-]jdk ' openjdk-11-jdk - OpenJDK Development Kit (JDK) openjdk-8-jdk - OpenJDK Development Kit (JDK) openjdk-13-jdk - OpenJDK Development Kit (JDK) openjdk-16-jdk - OpenJDK Development Kit (JDK) openjdk-17-jdk - OpenJDK Development Kit (JDK) 

Then just install by apt-get , Example:

apt-get install -y openjdk-17-jdk 

Troubleshooting

update-alternatives: command not found

When run update-alternatives , got command not found error:

# update-alternatives bash: update-alternatives: command not found 

update-alternatives is available in apt , use apt-get install to install:

apt-get install -y update-alternatives 

References

OmniLock — Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps

Источник

Читайте также:  Linux mint обновление драйверов wifi
Оцените статью
Adblock
detector