How to change java version on windows

How to install multiple Java versions on Windows in parallel? How to change Java versions on the command line? (Up to JDK 19)

In this article, I will show you how to install multiple Java versions on Windows and how to change the Java version on the command line:

Multiple Java versions on Windows

To enable these Java version change commands on your system as well, follow this step-by-step guide.

Let’s go…

Step 1: Installing Multiple Java Versions

Installing multiple Java versions in parallel 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)JavaSoftJRE1.2 by default – I recommend changing this to C:Program Files (x86)Javajdk1.2.2 and C:Program Files (x86)Javajre1.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)Javajdk1.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)Javajdk1.4.2_19.

Starting with the following versions, you don’t need to change the default installation directories:

  • Java SE 5
  • Java SE 6
  • Java SE 7
  • Java SE 8
  • Java SE 9 / OpenJDK 9
  • Java SE 10 / OpenJDK 10 (→ The most important new features in Java 10)

Attention – you may use the following Oracle distributions only for private purposes and development:

  • Java SE 11 / OpenJDK 11 (→ The most important new features in Java 11)
  • Java SE 12 / OpenJDK 12 (→ The most important new features in Java 12)
  • Java SE 13 / OpenJDK 13 (→ The most important new features in Java 13)
  • Java SE 14 / OpenJDK 14 (→ The most important new features in Java 14)
  • Java SE 15 / OpenJDK 15 (→ The most important new features in Java 15)
  • Java SE 16 / OpenJDK 16 (→ The most important new features in Java 16)
  • Java SE 17 / OpenJDK 17 (→ The most important new features in Java 17)
  • Java SE 18 / OpenJDK 18 (→ The most important new features in Java 18)
  • Java SE 19 / OpenJDK 19 (→ The most important new features in Java 19)

The following version is currently an early access build. You should use it only for testing purposes:

  • JDK 20 Early-Access Build (→ The most important new features in Java 20)

Step 2: Define Java Environment Variables

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 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).

Manually Setting the Java Environment Variables

The Java installers 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:

Opening Windows environment variables
Opening Windows environment variables

At this point, you can press «Enter» to open the system properties:

Windows 10 System Properties
Windows 10 System Properties

Click on «Environment Variables…» and the following window opens:

Windows environment variables Java 19
Windows environment variables Java 19

As the default version, I recommend the current release version, Java 19. Accordingly, 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 FilesJavajdk-19». 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» (if they exist):
    • C:ProgramDataOracleJavajavapath
    • C:Program Files (x86)Common FilesOracleJavajavapath
  • Insert the following entry instead:
    • %JAVA_HOME%bin

The entry should then look like the following (the other entries in the list will probably look different for you since you have other applications installed than I do):

Adding "%JAVA_HOME%bin" to the "Path" system variable
Adding «%JAVA_HOME%bin» to the «Path» system variable

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. But don’t worry – the scripts you can download in the next step will do that automatically.

How to Check Your Java Version on Windows

Now open a command line to check the settings with the following commands:

echo %JAVA_HOME% java -version

Code language: plaintext (plaintext)

Here’s what you should see:

Check your Java version with "cmd"
Check your Java version with «cmd»

Step 3: Install the Scripts to Change the Java Version

To change the Java version on the command line, I have prepared some batch files that you can copy to your system. Here is the link: scripts-up-to-java20-v2.zip

The ZIP file contains scripts named java20.bat, java19.bat, java18.bat, etc., for all Java versions, plus some common scripts starting with «javaX». I suggest you unpack the scripts to C:Program FilesJavascripts.

The scripts look like this:

java19.bat:

@echo off call javaX "Java 19"

Code language: DOS .bat (dos)

javaX.bat:

@echo off call javaX-JAVA_HOME %1 set Path=%JAVA_HOME%bin;%Path% echo %~1 activated.

Code language: DOS .bat (dos)

javaX-JAVA_HOME.bat:

@echo off if %1 == "Java 1.2" set JAVA_HOME=C:Program Files (x86)Javajdk1.2.2 if %1 == "Java 1.3" set JAVA_HOME=C:Program Files (x86)Javajdk1.3.1_28 ... if %1 == "Java 19" set JAVA_HOME=C:Program FilesJavajdk-19 if %1 == "Java 20" set JAVA_HOME=C:Program FilesJavajdk-20

Code language: DOS .bat (dos)

The scripts update the JAVA_HOME environment variable and insert the bin directory at the beginning of the Path variable. That makes it the first directory to be searched for the corresponding executable when you run Java commands such as java or javac.

(The Path variable gets longer with each change. Do not worry about it. This only affects the currently opened command line.)

Step 4: Add the Script Directory to the Path

To be able to call the scripts from anywhere, you have to add the directory to the «Path» environment variable (just like you did with «%JAVA_HOME%bin» in the second step):

Adding "C:Program FilesJavascripts" to the "Path" system variable
Adding «C:Program FilesJavascripts» to the «Path» system variable

If you have installed the latest releases of all Java versions, you can use the scripts without any further adjustments. Open a new command line and enter, e.g., the following commands:

Changing the Java version
Changing the Java version

If one of the commands does not activate the expected Java version, please check if the path in the javaX-JAVA_HOME.bat file corresponds to the installation path of the Java version you want to activate.

Temporary, Permanent, and System-Wide Java Version Changes

The commands presented up to this point only affect the currently opened command line. As soon as you open another command line, the default version defined in step 2 is active again (Java 19, if you have not changed anything).

That is why there are not one but three scripts for each Java version:

  • java<version>: Activates the Java version in the current command line.
  • java<version>-user: Sets the Java version as the default version for your user account.
  • java<version>-system: Sets the Java version as the default version for the entire system-

The -user variants of the scripts additionally set the JAVA_HOME environment variable with the setx command, permanently writing the change to the registry:

java19-user.bat:

@echo off call javaX-user "Java 19"

Code language: DOS .bat (dos)

javaX-user.bat:

@echo off call javaX-JAVA_HOME %1 setx JAVA_HOME "%JAVA_HOME%" set Path=%JAVA_HOME%bin;%Path% echo %~1 activated as user default.

Code language: DOS .bat (dos)

The -system variants also specify the /M parameter in the setx command. This sets the system-wide environment variable instead of the user-specific one:

java19-system.bat:

@echo off call javaX-system "Java 19"

Code language: DOS .bat (dos)

javaX-system.bat:

@echo off call javaX-JAVA_HOME %1 setx JAVA_HOME "%JAVA_HOME%" /M set Path=%JAVA_HOME%bin;%Path% echo %~1 activated as system-wide default.

Code language: DOS .bat (dos)

Attention: To set the system-wide Java version, you must open the command line as an administrator. Otherwise, you will get the error message «ERROR: Access to the registry path is denied.

What You Should Do Next…

I hope you were able to follow the instructions well and that the commands work for you.

Now I would like to hear from you:

Were you able to follow the steps well – or do you have unanswered questions?

Either way, let me know by leaving a comment below.

For Java applications, i.e. programs that are delivered (usually) as .jar files and started with java -jar xxx.jar or via a shortcut that does the same, the JRE that will be launched will be the first one found on the PATH.

If you installed a JRE or JDK, the likely places to find the .exes are below directories like C:Program FilesJavaSoftJREx.y.z. However, I’ve found some «out of the box» Windows installations to (also?) have copies of java.exe and javaw.exe in C:winntsystem32 (NT and 2000) or C:windowssystem (Windows 95, 98). This is usually a pretty elderly version of Java: 1.3, maybe? You’ll want to do java -version in a command window to check that you’re not running some antiquated version of Java.

You can of course override the PATH setting or even do without it by explicitly stating the path to java.exe / javaw.exe in your command line or shortcut definition.


If you’re running applets from the browser, or possibly also Java Web Start applications (they look like applications insofar as they have their own window, but you start them from the browser), the choice of JRE is determined by a set of registry settings:

Key: HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment
Name: CurrentVersion
Value: (e.g.) 1.3

More registry keys are created using this scheme:

(e.g.) 
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.3   
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.3.1

i.e. one for the major and one including the minor version number. Each of these keys has values like these (examples shown):

JavaHome    : C:program FilesJavaSoftJRE1.3.1
RuntimeLib  : C:Program FilesJavaSoftJRE1.3.1binhotspotjvm.dll
MicroVersion: 1

… and your browser will look to these settings to determine which JRE to fire up.

Since Java versions are changing pretty frequently, there’s now a «wizard» called the «Java Control Panel» for manually switching your browser’s Java version. This works for IE, Firefox and probably others like Opera and Chrome as well: It’s the ‘Java’ applet in Windows’ System Settings app. You get to pick any one of the installed JREs. I believe that wizard fiddles with those registry entries.

If you’re like me and have «uninstalled» old Java versions by simply wiping out directories, you’ll find these «ghosts» among the choices too; so make sure the JRE you choose corresponds to an intact Java installation!


Some other answers are recommending setting the environment variable JAVA_HOME. This is meanwhile outdated advice. Sun came to realize, around Java 2, that this environment setting is

  1. unreliable, as users often set it incorrectly, and
  2. unnecessary, as it’s easy enough for the runtime to find the Java library directories, knowing they’re in a fixed path relative to the path from which java.exe or javaw.exe was launched.

There’s hardly any modern Java software left that needs or respects the JAVA_HOME environment variable.


More Information:

  • http://java.sun.com/j2se/1.3/runtime_win32.html
  • http://www.rgagnon.com/javadetails/java-0604.html

…and some useful information on multi-version support:

  • http://www.rgagnon.com/javadetails/java-0604.html


Download Article


Download Article

Most of the time as java developer, one of the first thing you do to set up
on you new computer is java environment. This is basically telling your cmd (windows) terminal (mac or Linux) where to find the java.

java / javac (the compiler) is all under your java installation directory.

We assume that you have successfully downloaded and installed the JDK or JRE.
If you are a developer we suggest you to download the latest JDK (Java Developer Kit).

  1. Image titled Change Java Runtime Step 1

    1

    Click Start then right Click on «Computer» and choose «Properties.»

  2. Image titled Change Java Runtime Step 2

    2

    On the new window click «Advance system settings.»

    Advertisement

  3. Image titled Change Java Runtime Step 3

    3

    Choose «Environment Variables.»

  4. Image titled Change Java Runtime Step 4

    4

    Create variable with the following properties: name: JAVA_HOME , value: your path to jdk

  5. Image titled Change Java Runtime Step 5

    5

    Update the PATH variable by appending %JAVA_HOME%bin at the end

  6. Image titled Change Java Runtime Step 6

    6

    Click the Save button

  7. Image titled Change Java Runtime Step 7

    7

    Click Start > new > cmd

  8. Image titled Change Java Runtime Step 8

    8

    When you type following you should see the newly installed java version

    • java -version
  9. Advertisement

  1. Image titled Change Java Runtime Step 9

    1

    in your .bashrc or .bash_profile file append following

    • export JAVA_HOME=<your-java-dir>
    • export PATH=$PATH:$JAVA_HOME/bin
  2. Image titled Change Java Runtime Step 10

    2

    save the file

  3. Image titled Change Java Runtime Step 11

    3

    in terminal type:

    • . .bash_profile or . .bashrc (note that you have a space) this tells your terminal to load the updated file in current terminal so you get the newly created java in your path
  4. Image titled Change Java Runtime Step 12

    4

    You should see java version printed when you type following:

    • java -version
  5. Advertisement

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Thanks for submitting a tip for review!

About This Article

Thanks to all authors for creating a page that has been read 49,532 times.

Is this article up to date?

What Are We Gonna Explore

Oh Ya! Another new thing to learn.

Let’s roll, In this java tutorial or java tips, we will learn how to set java version and javac version.

The main reason is you might have a few different java versions installed in your machine. But Sometimes for some projects, we need to use some specific java version and javac version to build that project.

If you are curious enough to learn more about javac please click in this link – https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

Now If you know enough about javac then you must know why it should be the same. If you compile with Java8 and try to read with Java7. The result will drive you crazy.

So here is the thing. In my machine, I have JAVA7 & JAVA8.

java versions

Now please don’t ask me why I am still using windows7 and java7. Long story, in short, I needed it.

As you can see that in my machine I have JAVA7 & JAVA8 both. Now we have to make our JAVA version and JAVAC Version are the same.

when we type java -version and javac -version in cmd it should show us like this.

java version and javac version in cmd

According to my CMD result. I already have this set up correctly. If your CMD is not showing then please try to do the following things.

Checklist Item 1:

In your windows please find the configure java option to view or change java settings. click this configure java option.

configure java

This will open a Java Control Panel. Now you how to open java control panel.

java control panel

  1. Now click Java Option.
  2. Click View
  3. Now from this Enabled option, you can choose which version you want to use by simply check the checkbox from the enabled option.
  4. Or you can decide which version you don’t want to use by simply uncheck the checkbox from the enabled option.

Restart your CMD and See any changes or not from your previous output. If not please follow the Checklist Item 2.

Checklist Item 2:

Checklist item 2 and the most important things for your java and javac version are the system variables of your machine.

Okay, let’s see this scenario. In my System variables inside the path %JAVA_HOME%bin; was missing.

JAVA_HOME is set up as a = C:/Program Files/Java/jdk1.7.0_80 my desired JDK folder path.

let’s see what is the output came from CMD.

javac version is not recognised

Now you will see that

javac is not recognized as an internal or external command.

If you see clearly java version is also changed BTW still I have JAVA_HOME is set up with jdk1.7.0_80.
So inside the path, the JDK bin folder is really important to decide which javac will be used to compile your java code.

So follow this step to set up your JDK path

set java home and java bin

  1. in windows machine go to your computer -> properties > advanced system settings.
  2. click advanced and then Environment Variables
  3. under system variables set JAVA_HOME as your JDK folder path example: ” C:/Program Files/Java/jdk1.7.0_80
  4. then choose the path and then click edit and put your bin folder path in front as you see in the picture mentioned as 4. Make sure you put  ;  at the end.
  5. If you don’t want to add JAVA_HOME variable then put in the path like this  ” C:/Program Files/Java/jdk1.7.0_80/bin;

Now restart your cmd and try to see your java version and javac version.

java version and javac version in cmd

OHH YAAA !!! here you are.

If this step doesn’t help you to set the java version or javac version please let me know by comment below.i will find a way to help you. Thank you so much for reading this post.

If there is a mistake in this post or I missed any better way to do this please let me know by commenting below. I will appreciate your help and advice.

author in practice house

Java # JSF # Spring # Hibernate # PHP # Python # Dart # Flutter # MySql # Oracle  #JavaScript # jQuery # BootStrap 4 # CSS3 # HTML5


I love to explore new technologies. If you like my tutorials please share your thoughts about my work.
Check out my YouTube Channel For Video Tutorials.
To Hire Me Please Contact Me Through Social Media or https:www.amialif.com
If you want to help others by doing donation please Donate To These Charities.
You also can connect with me via social media. Thanks

JEnv for Windows Version 2 is here.

A complete rewrite of V.1

Change your current Java version with 3 words

  • JEnv allows you to change your current JDK Version.
  • This is helpful for testing or if you have projects requiring
    different versions of java
  • For example you can build a gradle project
    which requires java8 without changing your environment variables and
    then switch back to work with java15
  • It’s written in cmd and powershell so it can change the environment variables and can run on any Windows-10+.

I hope you enjoy it. Please give me a star if you like my work. Thank you!

Video Demo:

jenv

Installation

  1. Clone this repository
  2. Add it to the path
  3. Run jenv once so the script can do the rest
  4. If your using cmd you need to call the batch file. If you use powershell you should call /src/jenv.ps1
  5. Some reported problems putting JEnv into their C:/Programs folder due to required admin rights
  6. I hope I could help you. Else open an issue

Usage (Note: local overwrites change. use overwrites local)

  1. Add a new Java environment (requires absolute path)
    jenv add <name> <path>
    Example: jenv add jdk15 D:ProgrammeJavajdk-15.0.1

  2. Change your java version for the current session
    jenv use <name>
    Example: jenv use jdk15
    Environment var for scripting:
    —PowerShell: $ENV:JENVUSE="jdk17"
    —CMD/BATCH: set "JENVUSE=jdk17"

  3. Clear the java version for the current session
    jenv use remove
    Example: jenv use remove
    Environment var for scripting:
    —PowerShell: $ENV:JENVUSE=$null
    —CMD/BATCH: set "JENVUSE="

  4. Change your java version globally
    jenv change <name>
    Example: jenv change jdk15

  5. Always use this java version in this folder
    jenv local <name>
    Example: jenv local jdk15

  6. Clear the java version for this folder
    jenv local remove
    Example: jenv local remove

  7. List all your Java environments
    jenv list
    Example: jenv list

  8. Remove an existing JDK from the JEnv list
    jenv remove <name>
    Example: jenv remove jdk15

  9. Enable the use of javac, javaw or other executables sitting in the java directory
    jenv link <Executable name>
    Example: jenv link javac

  10. Uninstall jenv and automatically restore a Java version of your choice
    jenv uninstall <name>
    Example: jenv uninstall jdk17

  11. Automatically search for java versions to be added
    jenv autoscan [—yes|-y] ?<path>?
    Example: jenv autoscan "C:Program FilesJava"
    Example: jenv autoscan // Will search entire system
    Example: jenv autoscan -y "C:Program FilesJava" // Will accept defaults

How does this work?

This script creates a java.bat file that calls the java.exe with the correct version
When the ps script changes env vars they get exported to tmp files and applied by the batch file
An additional parameter to the PowerShell script was added. «—output» alias «-o» will create the tmp files for the batch. See images below

SystemEnvironmentVariablesHirachyShell

SystemEnvironmentVariablesHirachyPowerShell PNG

Contributing

If you want to contribute feel free to do so. This is a great repository for beginners as the amount of code is not huge and you can understand how it works pretty easily.
For running tests I suggest you to use the latest version of powershell (pwsh.exe):
https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.2
Be careful you have to run it as pwsh and not as powershell
Then you have to install Pester. This is only for tests: Install-Module -Name Pester -Force -SkipPublisherCheck
You could use your already installed powershell as well. However it has an old Pester Module already installed which you can not use and I could not figure out how it can be updated: pester/Pester#1201
Navigate into the test folder and run the test.ps1 file. It will backup your env vars and your jenv config while testing and automatically restore them later. But you should always let the tests finish else your vars and config wont be restored

java -version is running the wrong version of java.

Diagnostics:

>java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

the following is the Java related contents from the output of PATH:

PATH=C:ProgramDataOracleJavajavapath; ... C:Program FilesJavajdk1.6.0_45bin

Conclusion:

From the above output we can deduce that C:ProgramDataOracleJavajavapath is 1.8.0_66.

You need to change your PATH to put C:Program FilesJavajdk1.6.0_45bin first.

I noticed that after checking the path per your suggestion. Windows 10 does not allow me to edit the path because it says «This environment variable is too large.» I know there should be another question to deal with this separately.

You also need to clean up your path. My guess is you have a lot of duplicate entries.

I have the same problem, I have set JAVA_HOME:

C:Program FilesJavajdk1.7.0_75

and Path to:

%JAVA_HOME%bin

I need run jdk 7. When I run java -version it always appear jdk 8.

I solved it with: in System Environment —> Path —> order %JAVA_HOME%bin to first.

This is the REAL active JAVA executable into your PATH:

C:Program Files (x86)Common FilesOracleJavajavapath;

Remove it and the system take the value from

...;%JAVA_HOME%bin;

Tags:

Java

Windows 10

Related

Java is a high-level, robust, object-oriented, and secure programming language that was developed in the year 1995 by Sun Microsystems (which is now part of Oracle group), and James Gosling is known as the father of Java.  It is a general-purpose programming language intended to let programmers write once, and run anywhere, meaning that compiled Java code is compatible to run on all platforms that support Java without the need to recompile which is a major advantage.

Java Update

Java Update is a feature that keeps your windows computer up to date with the latest Java releases. You can use Java’s update feature to manually force an available update on windows. But, when you have auto update enabled, your system periodically checks for new versions of Java and updates it automatically.

Auto Update in Java

We can get the auto-update feature in Java

First, you need to check whether the windows check for update automatically you can do this by following the steps: 

  1.  Open the start by clicking on the windows logo on the bottom left side of the screen, and the start menu will pop up.
  2.  Now type configures Java and clicks on configure Java option from the list of matching programs, this will open Java control panel.
  3.  Once Java Control Panel is launched click on the update tab to access settings.
  4.  Select the Check for Updates Automatically check box for enabling Java updates to check for updates automatically.

Now, whenever an update is available you will receive a notification asking your permission to download the update, and a tooltip balloon appears on your Windows desktop. To install the new update, click on the notification dialog.

Update Notification for Java

Update Notification for Java

Now an Update Available dialog appears. To start the installation, click Update. Otherwise, you can click Later to install the update at a later time. When the installation is complete a dialog appears where you can click Finish to complete the installation.

An update Available dialog box

Using the Update Schedule, you can change the frequency for updates accordingly: daily, weekly, or monthly (default). To do this follow the given steps:

  1.  From the Java Control panel click on the Update tab, then select Advanced, now the Automatic Update Advanced Settings dialog will appear.
  2.  Here you can change the frequency and the date and time for the Java Update, finally click OK.

 Java Update scheduler will check for newer Java updates and notify you at the scheduled frequency.

Java Manual Update

1. Open the Start option by clicking the Windows logo in the bottom-left corner of the screen. The Start menu will pop up.

Main Screen with start option

2. Type in configure java in the search box and searches for matching programs will be displayed.

List of matches

3. Click on configure java option from the list of matching programs and the Java Control Panel will open.

Java Control Panel

4. Click on the update option in the Java control panel and then click on the Update now option at the bottom-right corner of the window and this initiates the Java to look for new updates.

Java Control Panel after clicking on the Update tab

5.  If Java finds an available update, follow any on-screen prompts that appear to confirm the update, then allow your computer to install the latest version of Java.

You cannot update to a new version of Java if you receive a dialog box saying that you are already running the latest version of Java.

This notification appears when you are using the latest Java version

 Finally, if you are not sure about the Java version then go to the official link to update to the latest version.

Понравилась статья? Поделить с друзьями:
  • Hotfix uninstall wizard windows 7 скачать
  • How to change icon in windows 10
  • Hotfix kb3033929 for windows 7 64 bit
  • How to change file type in windows 10
  • How to change fahrenheit to celsius in windows taskbar