Strange problem with environment variables


I have this strange Issue with environment variables on my windows 7 system. Its been there for an year and I have had a work around but I need to solve it now:

I have an environment variable JAVA_HOME set as C:\Program Files\Java\jdk1.6.0_39\. And in my PATH I have added %JAVA_HOME%\bin;.

But when I do java -version it prints "1.7.0_11" and when I do javac -version it prints as expected 1.6.0_39

I do not know why 1.7.0_11 is printed for java -version. I have checked my PATH and have added JAVA_HOME only once. How can I know which env variable is responsible for using my java.exe? How do I avoid it?

It's almost certainly a version of java.exe in c:\Windows\System32, which is likely to come earlier in your path than the part which refers to JAVA_HOME.