8

I am programming through java 1.6 u_17, but i have JRE version 6 and JRE version 7 installed, so how to run my compiled program from JDK 1.6 to run through the JRE 6 only?

By Default, it runs my class files through JRE 7, how to change this behavior? Any idea of setting Class path in windows 7, as we does it for JDK.

The following is shown in my command prompt

E:\JAVA>javac -version
javac 1.6.0_17

E:\JAVA>java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode, sharing)

This is for my SET command showing the Windows 7 environment paths

ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Administrator\AppData\Roaming
ClassPath=C:\Program Files\Java\jdk1.6.0_17\bin\mysql-connector-java-5.1.5-bin.jar;
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=RANDMATE-PC
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Administrator
JAVA_HOME=C:\Program Files\Java\jre6\bin;
LOCALAPPDATA=C:\Users\Administrator\AppData\Local
LOGONSERVER=\\RANDMATE-PC
NUMBER_OF_PROCESSORS=2
OS=Windows_NT              Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.6.0_17\bin;C:\Program Files\Java\jre6\bin;C:\Program Files\MyS
QL\MySQL Server 5.1\bin;C:\Program Files\UltraEdit\;C:\Program Files\Jar2Exe                  Wizard\;C:\Program Files\Java\jre6\bin;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 6 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=0605
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\ADMINI~1\AppData\Local\Temp
TMP=C:\Users\ADMINI~1\AppData\Local\Temp
USERDOMAIN=RANDMATE-PC
USERNAME=Administrator
USERPROFILE=C:\Users\Administrator
windir=C:\Windows
Rand Mate
  • 453
  • 3
  • 8
  • 14
  • Check out the PATH variable in windows. You can modify that to point to where your desired Java version is located at. Or you can just run Java launcher with the location. – Bhesh Gurung Oct 30 '12 at 02:49
  • @Bhesh Gurung-I have initally set my path for java JDK 1.6 alone but i not set the path either for JRE 6 or 7. But my java classes runs through the latest version, Is there any fix or command that I can run without uninstalling the recent version? – Rand Mate Oct 30 '12 at 02:56

12 Answers12

12

Looks like you are using Windows. Please go the Environment Variables and define a user variable as:

    Right Click(My Computers) -> Advanced -> Environment Variables -> Add

    Variable Name = JAVA_HOME
    Variable Value = Root Folder of Java 1.6.0_17 installation

Also in the same screen, please check the PATH variable and make sure **bin** Folder of Java 1.6.0_17 installation is added in the path preferably in the beginning and remove the Java 7 from the path, if it is there.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • 1
    Already I set my path for JDK and JRE too. Then why still the problem persists. – Rand Mate Oct 30 '12 at 02:52
  • @RandMate: Most probably its getting multiple entries. Perform a cleanup as I mentioned. Please make sure only one folder(JDK) is in the path. If still issues, go to command prompt and type `path`. Share me the output. – Yogendra Singh Oct 30 '12 at 02:54
  • @RandMate Your `JAVA_HOME` is wrong. Update it with `C:\Program Files\Java\jdk1.6.0_17`. There are so many java bin in the path, remove them all. – Yogendra Singh Oct 30 '12 at 03:11
  • I have changed it to C:\Program Files\Java\jdk1.6.0_17\bin; But still E:\JAVA>javac -version javac 1.6.0_17 E:\JAVA>java -version java version "1.7.0_09" Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode, sharing) E:\JAVA> – Rand Mate Oct 30 '12 at 03:14
  • 1
    @RandMate No `bin` folder in Java_home. Java_Home goes up to root folder only i.e. `C:\Program Files\Java\jdk1.6.0_17`. I am suspecting some more entries in the path causing the issue. Can you please cleanup the entire path variable, leave only `Path = %JAVA_HOME%\bin` for a minute and try? Please Note: Open a new command prompt after changing the values and then try. – Yogendra Singh Oct 30 '12 at 03:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18764/discussion-between-rand-mate-and-yogendra-singh) – Rand Mate Oct 30 '12 at 03:28
  • 10
    My %Path% environment variable had "%SystemRoot%\system32;" in the beginning of it, which kept setting my "java -version" to 1.7*. Removing that portion of the Path variable value let my computer be set to java 1.6* – TrippinBilly Oct 23 '13 at 22:42
  • Thanks billy, that comment saved me, i just swapped %systemRoom%\system32 to the end of my path so java_home gets picked up first – Cole9350 Apr 25 '14 at 16:01
  • I had the same issue...had to remove "C:\ProgramData\Oracle\Java\javapath" from Path environment variable – Will Aug 08 '16 at 10:51
10

I fix this problem by removing the java.exe in system32 folder. And then in the cmd the java command refer to the jdk set in the path enviroment variable.

joksy82
  • 405
  • 2
  • 6
  • 13
5

java.exe is also available in C:\windows\system32 and in your case its version might be 1.7..

Since your path has C:\windows\system32 first. no matter how to set rest of the path, sonar always will load jre7. (refer to conf\wrapper.conf in Sonar directory, by default it has wrapper.java.command=java)

To fix this, change following property of the "wrapper.conf" file wrapper.java.command=C:\Program Files\Java\jdk1.6.0_XX\bin\java

this will fix the issue irrespective of the path variables.

rsingh25
  • 356
  • 3
  • 9
4

For those who had the same problem I suggest reading this thread:

https://superuser.com/questions/479889/wrong-java-version-being-reported

Quote of wmz: "To check what actually executes when you run 'java -version' run from command line: for %I in (java.exe) do @echo %~$PATH:I"

Community
  • 1
  • 1
2

Windows is checking the PATH variable to find an executable named java and using that to launch your application. It is quite normal in a development environment to have many installations (as you do). However this calls on you to manage your environment.

There are two ways for you to set environment variables in Windows. By going to My Computer > Advanced Settings > Environment Variables as described above, you can set a variabler to your JDK installation and then update the Path to find the bin directory in that installation. Youy have the choice of updating System variables (all users) or user variables (your account only).

Personally, I prefer not to use the system/user environment variables as this provides less flexibility. You can use bat files to explicitly set the Path for a specific moment in time, or indeed to override the JAVA_HOME to point to a different installation.

If you are using an IDE you can usually specify the target runtime on a per project basis. Alternatively, when running the program, provide the fully qualified path to the version of the java executable you wish to use.

Romski
  • 1,912
  • 1
  • 12
  • 27
  • @Romski-Iam running Java via command prompt in windows 7. I'm not an expert in using commands. could you please suggest me an example command. – Rand Mate Oct 30 '12 at 03:24
  • 2
    If you don't want to rely on Windows deciding which version of java.exe will be run, you can explicitly call the version you want. So to run a HelloWorld class using different versions of Java you would type "c:\java\jdk6\bin\java HelloWorld", or "c:\java\jdk7\bin\java HelloWorld". Obviously, you would need to change the actual paths to suit your environment. – Romski Oct 31 '12 at 02:39
2

I had same problem. Issue is that Java installation will copy a java.exe, javaw.exe and javaws.exe to windows/system32 directory. You must have installed JDK7 recently.

I just removed those files from Windows/System32 directory, however, kept older version in path. This solved the issue.

Try it out.

Giri
  • 21
  • 1
2

I was facing the similar problem. I am using Java 7 and Java 8 together. The Java 8 was installed in the machine but the 7 set-up I copied from another system. I have already set the JAVA_HOME & path pointing to 7. But when I try to echo %path%, it shows the Jdk 8 in the class path. after lots of google, I have found "C:\ProgramData\Oracle\Java\javapath" in the path. Removing the line from the path solved the problem.

Sonu
  • 712
  • 9
  • 7
2

Background

I'm using Windows 8.1 and I have both JDK6 and JDK8 installed. For development purpose, I needed to get java version "1.6.0_45" over the command java -version and javac 1.6.0_45 over javac -version. The second command was working as expected after setting JAVA_HOME as Home folder of JDK6 and adding %JAVA_HOME/bin% in Path. However the first command was still returning java version "1.8.0_71"

Troubleshooting

I could find the an entry in the Path environment variable which points to C:\ProgramData\Oracle\Java. This folder actually contains short cut to java.exe of JDK8.

Proper solution: Uninstall JDK 8

Workaround:Edit the short cut file so that it points towards java.exe inside %JAVA_HOME%\bin (Please replace the variable with actual folder)

0

set the PATH variable to your jdk6\bin directory. so that it will pick the javac and java from jdk6.

for setting the path variable go to Environment Variables. After setting the path variables you need to restart your command prompt. (NOT the computer).

rbhawsar
  • 805
  • 7
  • 25
  • @rbhawsar- If you see my question I have updated the existing paths setted in my Windows environment, So I cannot find my JRE 7, so where else can i change it to run through jre 6, I hope now you got clear what my problem is. – Rand Mate Oct 30 '12 at 03:20
  • if you are running through the command prompt. please echo path variable and make sure that correct path is being used `echo %PATH%` – rbhawsar Oct 30 '12 at 03:22
  • C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1. 0\;C:\Program Files\Java\jdk1.6.0_17\bin;C:\Program Files\Java\jre6\bin;C:\Program Files\MySQL\My SQL Server 5.1\bin;C:\Program Files\Java\jdk1.6.0_17\bin;\bin – Rand Mate Oct 30 '12 at 03:35
  • `C:\Progra~1\Java\jdk1.6.0_17\bin` try putting this. I was just wondering why its not picked up. – rbhawsar Oct 30 '12 at 03:39
0

Just right click on MyComputer, then Advanced System Settings-->Environment Variables-->New Then in the "variable Name" text field add the string "PATH" or if it already exists select PATH from the list and click edit then append the path for "bin" folder in java in program files to the Variable value text field followed by a semicolon (;), if not exits add the path of bin directory of your java folder in "Variable Value" field and click "OK".

If you want to check the result, open up a command prompt and type "java" then hit enter.

Gopakumar N G
  • 1,775
  • 1
  • 23
  • 40
0

For windows 7(X86) path setting:

first step: Mycomputer(Right click)-->properties-->advanced settings-->environment variales-->new (user variable)

on this first varible name: "" JAVA_HOME "" value: "" C:\Program Files\Java\jdk1.7.0_45 ""

second variable name: "" PATH "" value: "" C:\Program Files\Java\jdk1.7.0_45\bin;.; ""

0

There are two types of path for java : Permanent & Temporary.

Here is the video tutorial describing how to set path for Java?

How to Set path for Java : https://www.youtube.com/watch?v=rM4Qq3Ua7Sk

This video tutorial will guide you in setting permanent and temporary path for Java. Simply follow the steps. This will work with any version of Java, JDK or JRE. In video I have used path of JDK, Simply copy path of JRE instead of JDK and rest is same.

OR read the tutorial here : How to Set path for Java : http://aatul.me/2011/08/24/how-to-set-path-for-java/

Hope this solutions will help you.