>

Thursday, July 30, 2015

Switching Java Home Variable for Java Different Version on Windows

For years I didn't write any post on my blog, now have a little times, I will write the simplest post. The topic is how to change JAVA_HOME and PATH variable in windows. Sometime we are dealing with between project and another project which are using different java version, but we do not want bother to switch setting java home variable environment for each java version.
On windows operating system we can create simple script with extension (.bat )that can override the java JAVA_HOME and PATH.
  • The requirement what we need here just notepad
  • A little window batch scripting syntax set and echo


The script is shown  below:
---------------------------------------------------------


@echo off
echo Setting JAVA_HOME
set JAVA_HOME =C:\JAVA\jdk1.7.51
echo setting PATH
set PATH=C:\JAVA\jdk1.7.51\bin;%PATH%
echo Display java version
java –version

--------------------------------------------------------

The explanation:
echo is for display the message on command line
set is to declare the environment variable that we want to change or to override
java –version is syntax to check  whether  the JAVA_HOME is already change or not.

Then we save as  on notepad as file with extension .bat for example is switching-java-variable.bat

how to run :

we can put the anywhere the file location, I put on drive D. let's run our script.

the result  is shown in the image above.

the script it was just simple script, beside you can implement on java, we can implement on ant and also maven. sometime we need oldest version.

note :
feel free to let me know if there were something that I have missed