- How to switch Java versions in Windows (updated 03/2021 for Java 16)
- Installation of multiple Java versions
- Download sources
- Environment variables
- Setting environment variables manually
- Scripts for changing the Java version
- How to check JDK version that installed on your computer
- 1. Where JDK is installed?
- How to Check Java Version on Mac or Windows
- PHP Code
- Check Java Version on Mac
- Option 1: Check Java Version on Mac Using the GUI
- Option 2: Check Java Version on Mac Using the Terminal
- How to check which version of Java you have installed in Windows 10, in 2 steps
- Check out the products mentioned in this article:
- Windows 10 (From $139.99 at Best Buy)
- Lenovo IdeaPad 130 (From $299.99 at Best Buy)
- How to check which Java version you have in Windows 10
- Getting Java version at runtime
- 12 Answers 12
- Sun Technical Articles
How to switch Java versions in Windows (updated 03/2021 for Java 16)
As a Java programmer, I work on various projects based on different Java versions, especially since the transition to the six-month release cycle. From time to time, I also have a piece of Java code that I want to try out on different Java versions – on the command line without having to click through the menus of my IDE. So it is helpful to be able to quickly and easily change the Java version to be used for compiling code or executing class files.
In this article, I’ll show you how to install several Java versions simultaneously in Windows and how to switch between them on the command line with short commands.
Installation of multiple Java versions
Installing several Java versions at the same time is incredibly easy in Windows. You can download and run the installer for each version, which automatically installs the versions in separate directories.
Download sources
- Java SE 1.1 – You can no longer install this version on 64-bit Windows.
- Java SE 1.2 – Installed to C:\jdk1.2.2\ and C:\Program Files (x86)\JavaSoft\JRE\1.2\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.2.2\ and C:\Program Files (x86)\Java\jre1.2.2\ for the sake of clarity.
- Java SE 1.3 – Installed to C:\jdk1.3.1_28\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.3.1_28\ .
- Java SE 1.4 – Installed to C:\j2sdk1.4.2_19\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.4.2_19\ .
Starting with the following versions, you don’t need to change the default installation directories:
Attention – you may use the following Oracle distributions only for private purposes and development:
The following version is currently an early access build. You should use it only for testing purposes:
Environment variables
In most cases, the following two environment variables decide which Java version an application uses:
- JAVA_HOME – many start scripts use this variable.
- Path – is used when running a Java binary (such as java and javac ) from the console.
These variables should always point to the same Java installation to avoid unforeseen problems due to inconsistencies. Some programs, such as Eclipse, define the Java version in a separate configuration file (for Eclipse, for example, this is the entry «-vm» in the eclipse.ini file).
Setting environment variables manually
The installers of the Java versions listed above already create various environment variables, which you need to clean up first (see below). The fastest way to change the environment variables is to press the Windows key and type «env» – Windows then offers «Edit the system environment variables» as a search result:
At this point, you can press «Enter,» and the system properties appear:
Here you click on «Environment Variables…», after which the following window opens:
As the standard version, I currently use the latest release version, Java 16. Therefore, you should make the following settings:
- The top list («User variables») should not contain any Java-related entries.
- The lower list («System variables») should contain an entry «JAVA_HOME = C:\Program Files\Java\jdk-16\» (please check the version number, you may have a newer one). If this entry does not exist, you can add it with «New…». If it exists, but points to another directory, you can change it with «Edit…».
- Delete the following entries under «Path»:
- C:\ProgramData\Oracle\Java\javapath
- C:\Program Files (x86)\Common Files\Oracle\Java\javapath
- Insert the following entry instead:
- %JAVA_HOME%\bin
The path list should then look like this:
The last entry ensures that Path and JAVA_HOME are automatically consistent. Attention: this only works for the default setting configured here. If you change JAVA_HOME via the command line, you have to adjust Path accordingly (more about this below).
Now open a command line to check the settings with the following commands:
As a result, you should see this:
Scripts for changing the Java version
Finally, we come to the possibility to change the Java version quickly and easily. Therefore I create a separate command for each Java version, called, for example, java16 , java15 , java14 , java13 . I put these commands in the directory C:\Program Files\Java\scripts , which I add to the environment variable «Path» as described before:
The files are named, for example, java16.bat , java15.bat , java14.bat , java13.bat , and change the environment variables as shown in the following example of java16.bat :
This script, admittedly kept very simple, inserts the bin directory of the Java version to be activated at the beginning of the Path variable, so that is where, for example, java.exe is looked for first. The path variable becomes longer with each switching. However, since this is only the case for the currently opened command prompt, I don’t think this is a problem.
The following ZIP file contains all my scripts from Java 1.2 to Java 17 (you might have to adopt one or the other Java directory to your installation): scripts-up-to-jdk17.zip
You can quickly test whether the scripts work in a command line. Attention: after adding the scripts directory to the Path variable, you have to open a new command line.
At this point, the setup is complete. I hope this article was helpful for you, and I’d love to hear your comments. If you liked the article, I’d be happy if you shared it via one of the following share icons.
How to check JDK version that installed on your computer
The Java development kit (JDK) contains tools for Java development, and the Java Runtime Environment (JRE) contains a JVM to convert byte code .class to machine code, and execute it, in short, the JRE runs Java program.
Check JDK Version
We can use javac -version to find out the version of the installed JDK. In the below example, the JDK version is 11.0.7
Check JRE Version
We can use java -version to find out the version of the installed JRE. In the below example, the JRE version is 1.8.0_252
The JDK and JRE versions can be different on the same computer. Multiple JDK and JRE versions are allowed on the same computer; it is better to find out which version is configured in the system classpath to run or compile the Java program.
1. Where JDK is installed?
The JDK also contains a JRE to run the Java program.
1.1 On Ubuntu or Linux, we can use which javac to find out where JDK is installed.
In the above example, the JDK is installed at /usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/ .
1.2 On Windows, we can use where javac to find out where JDK is installed.
Do I need JDK or JRE?
For end-users, they need to install JRE to run the Java program, and the JDK is for developers. For the production environment, the deployment team only need to install JRE to run the Java program. However, developers often request to install the JDK, instead of the standalone JRE on the production server, because the JDK contains JRE and also extra tools to monitor and debug the running Java program.
How to Check Java Version on Mac or Windows
PHP Code
- Home
- SysAdmin
- How to Check Java Version on Mac or Windows
Introduction
Java runs many of the applications that we use daily. Many applications cannot run on older versions of Java. Hence, it is essential to know which version is installed on your system.
In this tutorial, learn how to check the Java version on your Mac or Windows system.
Prerequisites
- A system running macOS or Windows
- Access to the command-line/terminal window
- A version of Java installed
Check Java Version on Mac
You can find the version of Java on macOS by:
- using the GUI (Mac’s System Preferences)
- prompting for the version in the terminal window
Option 1: Check Java Version on Mac Using the GUI
To check the Java version on Mac without using the terminal by searching for the Java Control Panel in the System Preferences.
1. Click the Apple icon in the menu bar to open the drop-down menu and navigate to System Preferences.
2. A new window opens with various icons and settings. Find and click the Java icon to open the Java Control Panel.
3. Once the Java Control Panel opens, click the About button. A new window with Java version information should appear.
Option 2: Check Java Version on Mac Using the Terminal
If you prefer using the terminal, checking the Java version is a simple one-command process.
1. First, open the terminal. To do so, click the Spotlight Search button in the top-right of the screen.
2. Type terminal in the search bar and open it by clicking on the icon in the search results.
3. Once in the command line, run the command: java -version . The output should display the Java version installed on your MacOS.
To check the version of the primary Java compiler – javac (pronounced “java-see”) use the command:
Note: If you have a Linux system, refer to How To Check Java Version Installed On Linux.
How to check which version of Java you have installed in Windows 10, in 2 steps
Java is a programming language that’s used to install and run programs. Chances are that there are already several programs on your Windows computer that run using Java, even if you don’t know it.
However, if Windows isn’t running the latest version of Java, you may have trouble downloading new programs, or even opening some websites.
To check which version of Java you’re running, you’ll need to use Windows 10’s Command Prompt app. The Command Prompt can seem intimidating at first, but if you know what to type, you’ll be fine.
Here’s everything you need to know.
Check out the products mentioned in this article:
Windows 10 (From $139.99 at Best Buy)
Lenovo IdeaPad 130 (From $299.99 at Best Buy)
How to check which Java version you have in Windows 10
1. Type «Command Prompt» into the search bar next to your Start menu, and click on it when it appears in the search results.
2. Type «java -version» into the Command Prompt, then press Enter on your keyboard.
After a moment, your screen should display the information your computer has about Java, including what version you have installed.
If nothing displays, it most likely means that you don’t have Java installed on your computer at all. Luckily, you can download it for free here.
Getting Java version at runtime
I need to work around a Java bug in JDK 1.5 which was fixed in 1.6. I’m using the following condition:
Will this work for other JVMs? Is there a better way to check this?
12 Answers 12
These articles seem to suggest that checking for 1.5 or 1.6 prefix should work, as it follows proper version naming convention.
Sun Technical Articles
- J2SE SDK/JRE Version String Naming Convention
- Version 1.5.0 or 5.0?
- «J2SE also keeps the version number 1.5.0 (or 1.5) in some places that are visible only to developers, or where the version number is parsed by programs»
- » java.version system property»
- «J2SE also keeps the version number 1.5.0 (or 1.5) in some places that are visible only to developers, or where the version number is parsed by programs»
- Version 1.6.0 Used by Developers
- «Java SE keeps the version number 1.6.0 (or 1.6) in some places that are visible only to developers, or where the version number is parsed by programs.»
- » java.version system property»
- «Java SE keeps the version number 1.6.0 (or 1.6) in some places that are visible only to developers, or where the version number is parsed by programs.»
java.version is a system property that exists in every JVM. There are two possible formats for it:
- Java 8 or lower: 1.6.0_23 , 1.7.0 , 1.7.0_80 , 1.8.0_211
- Java 9 or higher: 9.0.1 , 11.0.4 , 12 , 12.0.1
Here is a trick to extract the major version: If it is a 1.x.y_z version string, extract the character at index 2 of the string. If it is a x.y.z version string, cut the string to its first dot character, if one exists.
Now you can check the version much more comfortably: