Java set current version

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

Источник

Changing current version of Java within Windows

I’m working in a Windows XP environment and have recently installed java 1.6 because it was required by an application. However I don’t want this to be the default version of java to be used. How do I set it so that the command java -version will return 1.5.x

I would take a look at path environment variable. You can examine it in command line with: echo %PATH% . These are default folders where binaries are looked for. If you find a path to JAVA you could adjust it easily in System Preferences.

7 Answers 7

Change your PATH variable so that it has the location of the jdk5/bin directory:

  1. Start -> Control Panel -> System -> Advanced
  2. Click on Environment Variables, under System Variables, find PATH, and click on it.
  3. In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
  4. Close the window.
  5. Reopen Command prompt window, and run java -version

That’s because when you install Java, the bins are put into C:\WINDOWS\system32, which is part of your PATH. You should add the jre/bin directory to the beginning of the PATH.

If you have multiple JREs installed, the only way to achieve this is by changing the registry entry. Especially because there are tools which read the JRE info from registry and not the class path.

set JAVA_HOME=C:\jdk1.6.0u24 set PATH=%JAVA_HOME%\bin;%PATH% 

That will temporarily set up the environment in the command shell. Maven, Ant, etc. will pick up on your new version of Java without having to go to the Control Panel repeatedly.

Tools like Eclipse should be able to select which JDK to use in their own configuration tools for use within their environments.

Java 8 creates three shortcuts on \ProgramData\Oracle\Java\javapath that point to the latest Java8 java.exe, javaw.exe and javaws.exe and then puts \ProgramData\Oracle\Java\javapath at the front of the PATH so that no matter what you do to the JAVA_PATH environment variable, you still get the latest Java 8.

You can get around this by

1) renaming \ProgramData\Oracle\Java\javapath to something else (\ProgramData\Oracle\Java\javapath8 for example)

2) creating a new javapath folder under Java and

3) creating the shortcuts that you need.

Just restore the javapath when you are done with the old versions and you get Java 8 back.

Short, but to the point, very good answer, this one explains more specifically how to generate multiple versions of javapath folders, using symbolic links, so you could switch with no time between versions, and the registry stuff that is missing here, screenshots and trick to avoid reboot after changes too. see stackoverflow.com/questions/27996603.

There are two ways to fix this:

1) Change the PATH (as someone has already mentioned) The important thing with this solution is to set JAVA_HOME before the windows paths. This is because under the windows folder, there is a java.exe that redirects to the last installed jre.

2) Regedit. The key HKEY_LOCAL_MACHINE->SOFTWARE->JAVASOFT->Java Runtime Environment contains the last installed version that the java.exe in the windows folder redirects to. If you change this to a previously installed version, everything should be peachy. (At least, I think this is the right registry key)

Источник

Оцените статью