Java 6 and Java 7 on Windows
I am an Android Developer, and recently I started playing around with <a href="http://vertx.io/">vert.x</a>, if you haven't already, check it out. so I ran into some problems shortly after adding the necessary jar files to my project ... turns out vert.x requires Java 7. This was not to big of a problem for me on Mac OS X. Just run the Java 7 installer and ( to my surprise ) Eclipse found the new JDK. Java 6 was still my default Java installation which I was fine with.
Shortly, after I decided to give vert.x a shot on Windows 8 ( Samsung Series 7 Laptop, My new toy! :) ). So, first thing i did was run the installer I downloaded from <a href="http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html">here</a>. Of course the installer runs fine and my default Java installation becomes Java 7.
So, here is the reason I am writing this pro tip. I wanted a simple way to switch between Java 7 and Java 6 on my machine without to much of a headache.
From this point forward I am assuming you have installed both <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk6-downloads-1637591.html">Java 6</a> and <a href="http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html">Java 7</a> on your machine. For me both installers run perfect.
If you have some experience with java you know that JAVA_HOME is a common environment variable to set so that Java can be located on your machine. So, first we will go ahead and set that environment variable. Search for "Environment Variables" -> Settings -> Edit the System Environment Variables, which should popup a dialog something like this :
Inside this dialog you will want to add 3 Environment Variables to the System.
- JAVA6_HOME = { Location of Java 6 Installation }
- JAVA7_HOME = { Location of Java 7 Installation }
- JAVAHOME = %JAVA7HOME% - this variable will change, when you want to a different JDK.
It should look something like this :
After you have resolved the different JDK's on the system, you need to add %JAVAHOME%\bin to your PATH Environment Variable.
**NOTE : The windows installer's for Java put java.exe in your System32 folder so that you can run Java without setting environment variables. When adding %JAVAHOME%\bin to your PATH make sure that it appears before C:\Windows\System32, or you can delete the java.exe from C:\Windows\System32*
Path or PATH should now contain %JAVA_HOME%\bin :
Now, lets test it out.
Open a Command Prompt and type "java -version". You should a version number starting with "1.7.0_" .
Now lets go back to Java 6. Easy, just go back and change JAVAHOME from %JAVA7HOME% to %JAVA6_HOME% :
NOTE : Be sure to close your command prompt, because Environment Variables will not refresh without a restart of the Command Prompt
Open a Command Prompt and type "java -version". You should see a version number starting with "1.6.0_".
.
I realize it's not perfect, but it is easy enough for me. It is much easier than uninstalling / reinstalling the JDK's to make sure the right one is installed in C:\Windows\System32\
Written by Alex Boyd
Related protips
8 Responses
Hey Alex,
I have Windows 7 64bit and both Java 6 (jdk1.6.0_30) and Java 7 (jdk1.7.0) on my machine.. I tried your soluting, but it doesnt seem to work..after restarting my cmd window, it still says java 6.. Any More Inputs or help?
Because the solution as it is, is quite good..
Thanks Jan
It sounds like you may have missed this step :
NOTE : The windows installer's for Java put java.exe in your System32 folder so that you can run Java without setting environment variables. When adding %JAVAHOME%\bin to your PATH make sure that it appears before C:\Windows\System32, or you can delete the java.exe from C:\Windows\System32
If you don't add %JAVAHOME%\bin before C:\Windows\System32 then you will run into problems because the system java.exe will be run and not the one at %JAVAHOME%
Neither removing java.exe from System32 nor adding %JAVA_HOME%\bin works. Actually, removing java.exe in System32 makes java -version not work anymore. Any suggestion?
If you are working from cmd, you can set a temporary value to a environment variable.
set JAVAHOME = %JAVA7_HOME%
This makes %JAVAHOME%
give %JAVA7_HOME%
only in this session.
I keep getting this error message when I try to load the command prompt "java is not recognised as an internal or external command, operable program or batch file".
Any help on this would be greatly appreciated
thanks
That "The windows installer's for Java put java.exe in your System32" made my day: I was scraping around looking for what was overriding java executable in my path.
Thanks!
butter piece explanation.
worked exactly as you said (Y)
Thanks
Thanks Alex, it worked.