How to switch java version

Java on Windows: switching to a specific Java version/ runtime

This post show how to have multiple Java runtime environments on your PC and switch between them on Windows so that a particular program uses the version that you want it to.

Background

Due to Java SE Platform licensing changes by Oracle, developers have turned to alternatives such as OpenJDK. The AdoptOpenJDK site provides Prebuilt OpenJDK Binaries for Free (the main line on their landing page) and as you will see, there are numerous versions that you could use:

  • JDK 8 Long-term Support ( LTS )
  • JDK 11 LTS
  • JDK 16 (the latest)
  • Each of these could be 32-bit or 64-bit versions and you can choose the JVM that you run (Hotspot vs OpenJ9)

In short, there are options and there are times when you want to run something with a specific version of the JVM or you want to test something with multiple JVMs.

Installing and switching between multiple Java versions

The method that I follow is that I have a folder D:\apps where I put different applications tools that I want to use. Under this, I create a folder called jdk and use that as the main holding folder for all the different JDK / JVM that I want to use. I personally prefer not installin each JVM / JDK into my computer any more but to use them ‘without installation’.

Head on over to the releases page at AdoptOpenJDK to get the package you need. Start by changing the platform to Windows as shown below.

Next, pick the JDK (the full Java Development Kit) or the smaller JRE (Java Runtime Environment) and download the ZIP file version. Remember x86 means it’s the 32-bit version and x64 means that it’s the 64-bit version. If you have a modern (relatively new) Windows 10 PC, it’s almost certainly running the 64-bit version of Windows and will therefore support both the versions. In that case, the x64 is preferable though you may have some programs that don’t work correctly on the 64-bit version and need the 32-bit version.

Once you have downloaded the version (or versions) that you want to get, unzip the package. In my case, I unzip it to d:\apps\jdk so that I have all the JDKs/ JREs in one place. After unzipping, I change the folder names to be something like jdk-11.0.11_x64 and jdk-11.0.11_x86 so that I can easily tell which folder has the 64-bit and which one has the 32-bit.

Running a program with Java basically requires you to do: $ java . and for this to work, it needs two things to be in place:

  • The Java version that you want to run must be the first Java installation on the path (so that Windows can find it)
  • The environment variable JAVA_HOME must be set to the installation that you want to use
Читайте также:  Php перевернуть ассоциативный массив

So, for switching Java versions, you need to do this:

set JAVA_HOME=d:\apps\jdk\jdk-11.0.11_x86\ path=d:\apps\jdk\jdk-11.0.11_x86\bin;%PATH% 

The first line sets the JAVA_HOME and the second one moves the x86 version of the JDK / JRE to the path. Note that the JAVA_HOME points to the main directory where the whole installation is, and the path must be to the /bin folder under the JAVA_HOME so that java.exe can be found. After you do this, you should see this:

$ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK Client VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode) 

To switch to a different version, you just need to set both those items correctly again. For example, in my case, I would do:

$ path=d:\apps\jdk\jdk-11.0.11_x64\bin;%PATH% $ set JAVA_HOME=d:\apps\jdk\jdk-11.0.11_x64\ $ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode) 

(Notice how the last line shows 64-Bit Server VM since it’s now running the 64-bit VM)

It’s not as convenient as using pik to switch Ruby versions on Windows but I imagine it would be a good idea to write something like pik for this. Also, I did not search if there are tools that let you do this easily but there might well be something.

That’s all there is to it. I noted this down mainly for myself since I need to do this occasionally. If it helps you, that’s great! Please leave a comment if there is something else that you think we should add into the post.

Источник

How to change the default Java version on Ubuntu

If you are a Java developer, it is normal to have multiple Java versions installed on your machine to support different build environments. When a Java program is compiled, the build environment sets the oldest JRE version the program can support. Now, if you run this program on a Linux machine where an unsupported Java version is installed, you will encounter an exception.

For example, if your program is compiled on Java 11, it can’t be run on a machine where Java 8 is installed. But the good thing is you can install multiple Java versions on your machine and quickly change the default JRE version.

In this tutorial, I’ll explain how to change the default Java version on a Linux machine. First of all, run the following command to check the current Java version:

$ java -version openjdk version "1.8.0_191" OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.10.1-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode) 

As you can see above, the default Java version is currently set to OpenJDK JRE 1.8. Now, let’s run the following command to see all available Java versions:

$ sudo update-alternatives --config java 

Running the above command displays a list of installed Java JDKs and JREs allowing you to select the one as you want to set as default.

There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press enter> to keep the current choice[*], or type selection number: 

When prompted, select the Java version you would like to use. If the list does not include your desired Java version, you can always install it.

Now you can verify the default Java version as fellows:

$ java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.10.3) OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.10.3, mixed mode, sharing) 

That’s it. The default Java version is changed to OpenJDK 11.

If you frequently switch between different Java versions, it is a good idea to write a short script to automate the process. Here is the script I used for switching to OpenJDK 8 on my machine. java8.sh

sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/ export PATH=$PATH:$JAVA_HOME 

Similarly, you can create scripts for other Java versions installed on your machine. The next step is to add these scripts as aliases to .bashrc file.

... # Java Alias alias java8='source /opt/java/switch/java8.sh' alias java11='source /opt/java/switch/java11.sh' 

Read Next: How to install Java on Ubuntu 18.04 ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Switching Java Versions on the Command Line

With Java releases coming out every 6 months, many developers want to try out the new features included in each release. However, you probably won’t migrate all your projects to a new version yet and will want to maintain several versions of the JDK on your machine, switching Java versions when needed. In this article, we look at how to switch Java versions via the command line.

JRebel & Java

JRebel 2021.2 now supports Java 16. Try JRebel free for 10 days to see how it supports your JDK.

Start Free

Switching Java Versions Via the Command Line

In this post, I want to share my setup to switch the active JDK version on the command line. Note, I’m using a Mac, and the scripts in this post will work on a Mac and, perhaps, on some Linux machines. If you have a good recipe on how you switch Java versions on the command line on Windows, please share with the community in the comments.

Let’s get to it then. When you download a new JDK release it comes as an installer, so you double click it, click the «Next» button necessary amount of times, and it puts the files somewhere on the filesystem. Or you do it manually. To run Java command line utilities successfully, including the java command, you need two things:

Using the setjdk Function

Here’s how I do it, and if I’m not mistaken I took this approach from Neeme.

We’ll utilize the ~/.bashrc file to declare the functions we’ll use later. So open it in your favorite editor, something line atom ~/.bashrc would do.

The first function, which we’ll use later to set the JDK versions is setjdk.

# set and change java versions function setjdk() < if [ $# -ne 0 ]; then removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin' if [ -n "$" ]; then removeFromPath $JAVA_HOME fi export JAVA_HOME=`/usr/libexec/java_home -v $@` export PATH=$JAVA_HOME/bin:$PATH fi >

This script finds the correct $JAVA_HOME location by using the /usr/libexec/java_home utility passing the argument string as a parameter. Then it exports the $JAVA_HOME and the correct $PATH values.

Switching Between Java Versions

Since you’d like to switch the versions back and forth, we also include the code to clean up the $PATH, to remove the current version of the $JAVA_HOME/bin from it. Here’s what the removeFromPath function looks like. Oh, and don’t forget to add it to the ~/.bashrc too.

What it does is pretty self-explanatory, albeit quite cryptic. It uses sed to replace the argument value with an empty string. Essentially, it removes whatever you pass it from the $PATH.

Setting the Desired Java Version

Now with these functions in place you can set the desired versions of Java as active. Save the ~/.bashrc file. If you’re doing it in a terminal session, reload it with the source ~/.bashrc. Try now setting your default Java to one of the different Java versions you currently have installed.

→ setjdk 1.8 → java -version java version "1.8.0_91" Java(TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode) → setjdk 9 → java -version java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+174) Java HotSpot(TM) 64-Bit Server VM (build 9-ea+174, mixed mode)

It should work. If it doesn’t, debugging this small shell script is quite straightforward (hint: most probably it’s a typo, check yourself), so we’ll omit the debugging guide from this post for brevity. Oh, also, it might be a good idea to add the setjdk 1.8 line to the ~/.bashrc, so all your sessions start with a predetermined JDK version.

How Do I Check My Java Version?

If you want to check your Java version from the command line, use the command

java -version .

Additional Resources

Looking for additional Java resources? We cover all ends of the Java earth in our blog.

Also, our Java 8 cheat sheet puts all the basic stuff on a single page for easy reference. Download a copy by clicking the button below.

Источник

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