How to set java_home on Windows 7?
Asked Answered
E

18

441

I went to the Environment Variables in 'System' in the control panel and made two new variables, one for user variables and one for system variables. Both were named JAVA_HOME and both pointing to

C:\Sun\SDK\jdk\bin

But for some reason, I still get the below error when running a Java command...

BUILD FAILED
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:161: The following error  occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:141: The following error occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:137: Please set java.home to a JDK installation

Total time: 1 second
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish>lib\ant\bin\ant -f setup.xml
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib\tools.jar
Buildfile: setup.xml

How can I fix this problem?

Eskimo answered 12/4, 2010 at 2:37 Comment(7)
You closed cmd and reopened it? Also it looks like you are pointed to the JRE not the JDK. Have you installed the JDK?Defalcation
I have definitely installed the JDK and it's located in C:\Sun\SDK\jdk\bin which I have set java_home to.Eskimo
I've even restarted.........a few times since........Eskimo
Still it seems to be looking in C:\Program Files\Java\jre6\lib\ c Can you do echo %JAVA_HOME% in your command prompt and see what it reportsDefalcation
in my command prompt, when I ran echo %JAVA_HOME%, it returned: C:\Users\Derek\Desktop\eclipse\eclipse\glassfish>echo %JAVA_HOME% C:\Sun\SDK\jdk\binEskimo
In what context are you running cmd.exe?Defalcation
The solution is: Omit /binLuciennelucier
N
688

Find JDK Installation Directory

First you need to know the installation path for the Java Development Kit.

Open the default installation path for the JDK:

C:\Program Files\Java

There should be a subdirectory like:

C:\Program Files\Java\jdk1.8.0_172

Note: one has only to put the path to the jdk without /bin in the end (as suggested on a lot of places). e.g. C:\Java\jdk1.8.0_172 and NOT C:\Java\jdk1.8.0_172\bin !


Set the JAVA_HOME Variable

Once you have the JDK installation path:

  1. Right-click the My Computer icon on your desktop and select Properties.
  2. Click the Advanced tab, then click the Environment Variables button.
  3. Under System Variables, click New.
  4. Enter the variable name as JAVA_HOME.
  5. Enter the variable value as the installation path for the Java Development Kit.
  6. Click OK.
  7. Click Apply Changes.

Note: You might need to restart Windows

The complete article is here, on my blog: Setting JAVA_HOME Variable in Windows.

Noncompliance answered 29/6, 2011 at 13:27 Comment(9)
thanks. finally I realized that one has only to put the path to the jdk without /bin in the end (as suggested on alot of places). e.g. C:\Java\jdk1.6.0_31 and NOT C:\Java\jdk1.6.0_31\bin !Dressing
God forbid there was an installer that did this step for you :) I just switched from the Enterprise version to the Community version and I was convinced I downloaded the wrong thing because it was one-click on the Enterprise version :pUnclean
Sometimes, the system varible changes does not take effect until you restart the windows.Zygosis
A quicker method is to hit Start, then start typing environment variablesDominicadominical
Correct but opening the Environment Variables with the method listed in the answer will open the settings with admin privileges, whereas opening it from start menu won't allow you to add new or edit current 'System Variables'.Fireeater
/Java/jre* is not JDK directory, this is Java Runtime Enviroment diretory. JDK is in a separate directory unless you want to do some crazy hackOff
Another common mistake is that people who doesn't know what the environment variables are used for, tend to put a semicolon at the end of JAVA_HOME. This will effectively invalidate them, when used in conjunction with expressions like %JAVA_HOME%\bin. Do not use semicolons in your environment variables, unless you have to!Kenay
Instructions don't tally with Windows 7 specifically (in original question)... Win 7 instructions are found in another answer (currently with less votes). Points about JRE vs JDK location are useful though.Selwin
Even though you have added the JAVA_HOME variable, just ensure that your PATH has the same reference. In my case I had a distinct JAVA path in JAVA_HOME and PATH. Phew!Poole
N
167

What worked for me was adding the %JAVA_HOME%\bin to the Path environment variable with the JAVA_HOME environment variable pointing to the jdk folder.

Nuggar answered 11/6, 2010 at 17:4 Comment(0)
W
134

You have to first Install JDK in your system.

Set Java Home

JAVA_HOME = C:\Program Files\Java\jdk1.7.0 [Location of your JDK Installation Directory]

Once you have the JDK installation path:

  • Right-click the My Computer icon on
  • Select Properties.
  • Click the Advanced system setting tab on left side of your screen
  • Aadvance Popup is open.
  • Click on Environment Variables button.

enter image description here

  • Under System Variables, click New.
  • Enter the variable name as JAVA_HOME.
  • Enter the variable value as the installation path for the Java Development Kit.
  • Click OK.
  • Click Apply Changes.

Set JAVA Path under system variable

PATH= C:\Program Files\Java\jdk1.7.0; [Append Value with semi-colon]

check here

Wandawander answered 17/6, 2013 at 7:7 Comment(4)
Should JAVA_HOME contain "\bin"? Other answers here seem to suggest "bin" should be included for PATH but not for JAVA_HOME.Theatricals
JAVA_HOME should NOT contain \bin.Mnemonics
[PLUS ONE] Nice flowers!Haldes
But on Image you enter with \bin.Paly
D
36

In cmd (temporarily for that cmd window):

set JAVA_HOME="C:\\....\java\jdk1.x.y_zz"

echo %JAVA_HOME%

set PATH=%PATH%;%JAVA_HOME%\bin

echo %PATH%
Decompress answered 2/4, 2015 at 13:8 Comment(4)
True. If you use setx then the JAVA_HOME will still be set after you have shutdown your computer. Then you don't have to set it again. Also bin is not needed.Satinet
Yes, but if you don’t want to officially install Java, then as sgrillon said, you can set it temporarily for that instance. Better yet, create a batch file that you can run to open a command-prompt window and set JAVA_HOME, PATH, and CLASSPATH, then you can have a “portable” Java environment.Turk
It's true but " " are not needed for setting the JAVA_HOME addressHugh
@Aboozar Rajabi, You need " " if ... path contain a space char.Madeira
N
21

You need to set it to C:\Sun\SDK\jdk (Assuming that is where the JDK is installed - It is not the default) - Do not put the \bin in C:\Sun\SDK\jdk\bin.

If your app only runs when you are logged in as the current user then put it in the user variables - If it needs to run for all users on your system then put it in System variables.

You might also need to add %JAVA_HOME%\bin to the path also (Also it depends on whether you run it from just the user or from all users, including System)

Norvell answered 12/4, 2010 at 2:55 Comment(0)
U
17

http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html

Control Panel > Java, Java tab, click the View button. In Runtime Parameters, put:

-Djava.home=YOUR_PATH_HERE

Or when you execute Java you can add that command line switch to the command:

java -Djava.home=PATH SomeJavaApp
Unction answered 12/4, 2010 at 2:39 Comment(1)
Th control panel thing seems to apply for the commercial version of Java only. Not for the GPL version.Liber
M
11

One Image can fix this issue. enter image description here

For More

Macy answered 5/2, 2016 at 10:0 Comment(0)
B
7

This is the official solution for setting the Java environment from www.java.com - here.

There are solutions for Windows 7, Windows Vista, Windows XP, Linux/Solaris and other shells.


Example

Windows 7

  1. Select Computer from the Start menu
  2. Choose System Properties from the context menu
  3. Click Advanced system settings -> Advanced tab
  4. Click on Environment Variables, under System Variables, find PATH, and click on it.
  5. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  6. Reopen Command prompt window, and run your Java code.
Borries answered 17/8, 2012 at 4:14 Comment(2)
The official instructions are useless. They only give generic instructions on how to set environment variables rather than indicating the value that needs to be set. They simply say to set it to “the location of the class” which is confusing and meaningless.Turk
These instructions do not set JAVA_HOME, an entirely separate variable from PATH that is required for some programs.Abra
S
7

For those who are still stumped with this problem (I tried all the above suggestions) --

If you're on a 64-bit version of Windows and you've installed the 32-bit JDK, besides adjusting PATH variables, you may need to adjust registry variables, too.

I was pulling my hair out, having correctly set my PATH variables -- still to no avail -- and then only finding "vacated" Java entries in my registry, seemingly a deadend of fixing the "misfiring" Java Runtime Environment.

By using Process Monitor to watch the program I was trying to get started, in order to sniff out where it was looking in the registry for Java (Runtime Environment), I triumphantly discovered that it's looking in the 32-bit version of registry entries, found in HKEY_LOCAL_MACHINE\SOFTWARE\**Wow6432Node**\JavaSoft\Java Runtime Environment.

Within that key, you should find subkeys of different Java versions installed (past and/or present). Click on the subkey of the latest version (my subkey is currently 1.7.0_25, for example). After clicking on that subkey, you'll see registry string values listed on the right, and particularly, JavaHome and RuntimeLib. You need to modify the values of those two values to reflect the both the current folder and jvm.dll file, respectively.

For example, in my case, the values were (previously) respectively set at C:\Program Files (x86)\Java\jre7 and C:\Program Files (x86)\Java\jre7\bin\client\jvm.dll which are nonexistent on my machine. I had to update these to the current folder and file of C:\Program Files (x86)\Java\jdk1.7.0_25\jre and C:\Program Files (x86)\Java\jdk1.7.0_25\jre\bin\client\jvm.dll.

Again, this will depend entirely on both what version of Java (JDK and/or JRE) you have installed -- 32 or 64-bit -- and what type of operating system you're on -- 32 or 64-bit. Just know that they're reflected in different locations within the registry (like the Wow6432Node for 32 bit applications, in my case with the 32-bit JDK installed on a 64-bit machine).

Now that I've updated those two registry values, my program runs flawlessly, with no more hiccups or complaints about a missing Java Runtime Environment (stemming from the registry).

Silsby answered 26/6, 2013 at 5:47 Comment(4)
I am on 64-bit windows 7 machine and I have both : C:\Program Files (x86)\Java\ and C:\Program Files\Java\ Now my question is that which one should my java_home point to ?Levi
Interesting; I only have the Java folder beneath the x86 folder.Reinert
Under C:\Program Files (x86)\Java\jdk1.7.0_25\jre\bin I dont have directory client, it only exist under C:\Program Files (x86)\Java\jre7\bin. What should I do?Hotfoot
On windows 7, after searching for keyword "1.8.0_74", I found multiple registries using regedit in HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java Development Kit/1.8.0_74/ or ../1.8.0_65 and all other installed previously... After deleting one by one registry from bottom to my latest installed java 1.8.0_112, now YourKit java profiler (profiler.exe) works without error: "Error occured while starting C:\Program Files\Java\jdk1.8.0_74\bin\javaw.exe" .Gains
Z
6

Windows 7

  1. Go to Control Panel\All Control Panel Items\User Accounts using Explorer (not Internet Explorer!)

    or

    • click on the Start button

      start

    • click on your picture

      clickOnPhoto

  2. Change my environment variables

    Change my environment variables

  3. New...

    new

    (if you don't have enough permissions to add it in the System variables section, add it to the User variables section)

  4. Add JAVA_HOME as Variable name and the JDK location as Variable value > OK

    edit system variable ok

Test:

  • open a new console (cmd)
  • type set JAVA_HOME
    • expected output: JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
Zimmerman answered 15/1, 2016 at 10:11 Comment(0)
C
6

We need to make a distinction between the two environment variables that are discussed here interchangeably. One is the JAVA_HOME variable. The other is the Path variable. Any process that references the JAVA_HOME variable is looking for the search path to the JDK, not the JRE. The use of JAVA_HOME variable is not meant for the Java compiler itself. The compiler is aware of its own location. The variable is meant for other software to more easily locate the compiler. This variable is typically used by IDE software in order to compile and build applications from Java source code. By contrast, the Windows CMD interpreter, and many other first and third party software references the Path variable, not the JAVA_HOME variable.

Use case 1: Compiling from CMD

So for instance, if you are not using any IDE software, and you just want to be able to compile from the CMD, independent of your current working directory, then what you want is to set the Path variable correctly. In your case, you don't even need the JAVA_HOME variable. Because CMD is using Path, not JAVA_HOME to locate the Java compiler.

Use case 2: Compiling from IDE

However, if you are using some IDE software, then you have to look at the documentation first of all. It may require JAVA_HOME to be set, but it may also use another variable name for the same purpose. The de-facto standard over the years has been JAVA_HOME, but this may not always be the case.

Use case 3: Compiling from IDE and CMD

If in addition to the IDE software you also want to be able to compile from the CMD, independent of your current working directory, then in addition to the JAVA_HOME variable you may also need to append the JDK search path to the Path variable.

JAVA_HOME vs. Path

If your problem relates to compiling Java, then you want to check the JAVA_HOME variable, and Path (where applicable). If your problem relates to running Java applications, then you want to check your Path variable.

Path variable is used universally across all operating systems. Because it is defined by the system, and because it's the default variable that's used for locating the JRE, there is almost never any problem running Java applications. Especially not on Windows where the software installers usually set everything up for you. But if you are installing manually, the safest thing to do is perhaps to skip the JAVA_HOME variable altogether and just use the Path variable for everything, for both JDK and the JRE. Any recent version of an IDE software should be able to pick that up and use it.

Symlinks

Symbolic links may provide yet another way to reference the JDK search path by piggybacking one of the existing environment variables.

I am not sure about previous versions of Oracle/Sun JDK/JRE releases, but at least the installer for jdk1.8.0_74 appends the search path C:\ProgramData\Oracle\Java\javapath to the Path variable, and it puts it at the beginning of the string value. This directory contains symbolic links to the java.exe, javaw.exe and javaws.exe in the JRE directory.

So at least with the Java 8 JDK, and presumably the Java 8 JRE standalone, no environment variable configuration needs to be done for the JRE. As long as you use the installer package to set it up. There may be differences on your Windows installation however. Note that the Oracle JRE comes bundled with the JDK.

If you ever find that your Java JDK configuration is using the wrong version of the compiler, or it appears to be working by magic, without being explicitly defined so (without casting the spell), then you may have a symlink somewhere in your environment variables. So you may want to check for symlink.

Cassy answered 14/3, 2016 at 9:31 Comment(0)
C
5

goto Mycomputer(This PC) -> rightclick ->select properties -> Advanced system settings -> environment variables-> in system variables click "New" button and write JAVA_HOME in variable name and path C:\Program Files\Java\jdk1.8.0_131 were jdk is present in variable value-> click ok.

close and reopen the command prompt after setting JAVA_HOME. Sometimes changes does not reflect in the cmd opened before setting the JAVA_HOME.

you can also set JAVA_HOME through terminal itself: SET JAVA_HOME="C:\Program Files (x86)\Java\jdk1.8.0_131"

Citified answered 7/1, 2018 at 10:8 Comment(0)
D
4

In Eclipse: Window->Preferences->Java->Installed JREs

Use the search feature to make sure your latest Java installation is listed; then make sure it is the one that is checked. This should be a JDK not a JRE.

Donall answered 8/5, 2011 at 1:21 Comment(1)
These instructions do not set JAVA_HOME, which is not related to eclipse.Abra
U
4

While adding your Java directory to your PATH variable, you might want to put it right at the beginning of it. I've had the problem, that putting the Java directory at the end of the PATH would not work. After checking, I've found java.exe in my Windows\System32 directory and it looks like the first one wins, when there are several files with the same name in your PATH...

Ulita answered 23/5, 2013 at 14:49 Comment(2)
When/if you install Java with the installer, it will replace the copies of java.exe and javaw.exe in the system folders with the copies from the JRE/JDK, so it should still work. If you install it manually (just unpack the archives), then you will indeed have to watch out for which copy is being run.Turk
These instructions do not set JAVA_HOME, an entirely separate variable from PATH that is required for some programs.Abra
T
3

Run Eclipse as Administrator.

That solved my problem. I'm still digging for the logic behind it.

Thagard answered 12/8, 2010 at 12:0 Comment(2)
I can't say I understand this exactly, but maybe eclipse is actually just giving the wrong error message here. When you run it as a non-admin, it can't delete the .jar files or something and then falsely reports that it didn't find javac. Anyway, this solution worked for me too.Inquiring
if you are running Eclipse in windows server 2012 R2 as is in my case, running eclipse as administrator works for me. Remember also to set the java_home to the program files folder and not the program (86) files folder. Hope this one helps some one.Files
B
2

if you have not restarted your computer after installing jdk just restart your computer.

if you want to make a portable java and set path before using java, just make a batch file i explained below.

if you want to run this batch file when your computer start just put your batch file shortcut in startup folder. In windows 7 startup folder is "C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

make a batch file like this:

set Java_Home=C:\Program Files\Java\jdk1.8.0_11
set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_11\bin

note: java_home and path are variables. you can make any variable as you wish. for example set amir=good_boy and you can see amir by %amir% or you can see java_home by %java_home%

Brachiate answered 7/3, 2016 at 8:47 Comment(0)
S
0

After hours of work around most of the solutions here, the problem was solved for me just by installing 32-bit JDK.

Stotinka answered 16/7, 2013 at 19:42 Comment(1)
Which is fine if that's what you want, but some people prefer not to actually install Java and just set up a “portable” environment manually, in which case, you need to know what environment variables need to be set.Turk
F
0

You may wanna look into Windows/System32 folder for the following files: - java.exe - javaw.exe - javaws.exe

Because, the path variable is including these files, you can get a headache trying to figure out why the java -version or javac -version keeps pointing to the old JAVA_HOME: rename those files, take out the extension with an underscore, for instance. And then, you only will have to create the JAVA_HOME variable, add it to the path variable (e.g., %JAVA_HOME%) and append the "\bin" particle. This way you need to only change the JAVA_HOME variable pointing to different JRE or JDK and have multiple installations of java just by setting JAVA_HOME.

This also means that it is better to install Java manually, without an installer, so you are able to toy with it.

Fructification answered 27/2, 2019 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.