How to set environment variables in Windows 7 for Java

28 comments - This post in romanian

Environmental variables are used by the operating system to save settings (default values, locations of resources) to be used by Windows or by processes launched by users.
There are two types of environmental variables:

  • user variables that are specific to a particular Windows user account;
  • system variables are always visible, regardless of the used user account.

Even if these variables are usually defined and initialized automatically when you install the system or other applications, there are situations in which the user must manually define them to put at the disposal of applications.
The considered scenario is to set environment variables to enable the compilation and execution of Java applications from the command line (command prompt) or by using an IDE like Eclipse. By installing the Java SDK, system variables about the location of executables (compiler, java virtual machine) are not defined or initialized automatically.
Testing is done by opening command prompt (Start -> cmd) and trying to launch the compiler with the command

C:\Users\Catalin>javac.exe

If there is no system variable to indicate where to look for the this executable, the system will give an error like:

'javac' is not recognized as an internal or external command,
operable program or batch file.

The solution to this problem is given by setting the system variables: JAVA_HOME, PATH and CLASSPATH:

  1. Open the Control Panel -> System or Security –> System; the same thing can be done by right-clicking on MyComputer and choosing Properties

System window in Windows 7

System window in Windows 7


2.   Choose Advanced System Settings option
System properties window in Windows

System properties window in Windows


3.   Choose the Environment Variables option
Environment variables in Windows

Environment variables in Windows


4.   In the System variables section it is selected New

5.   Define the variable name, JAVA_HOME and its value C:\Program Files\Java\jdk1.6.0_16 (for this example JDK version 1.6.0 was installed in C:\Program Files\Java\jdk1.6.0_16 folder; if needed, modify this value to reflect the real situation)

6. Insert a new system variable named, CLASSPATH and its value %JAVA_HOME%\jre\lib

New system variable

New system variable


7. For PATH, if it already exists, select it and choose the Edit option; in the editor add the value;%JAVA_HOME%\bin (the new values are separated by a semicolon from the existing ones)

Testing the system variables is done by opening a new command prompt window (Start -> cmd) and trying to launch the compiler with the command:

C:\Users\Catalin>javac
Usage: javac
where possible options include:
  -g                         Generate all debugging info
...

or, by using next commands

C:\Users\Catalin>echo %CLASSPATH%
C:\Program Files\Java\jdk1.6.0_16\jre\lib
 
C:\Users\Catalin>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.6.0_16
 
C:\Users\Catalin>echo %PATH%

8. Restart the computer in order to make your system aware of these changes (thanks to Ike for reminding me)

, ,


  1. #1 by Ike on July 26th, 2011

    You forgot to say that you need to RESTRART the computer to invoke the changes.

    After restarting my PC more than half an hour, I found out that you only need to have ‘bin folder’ variable:
    PATH = C:\Program Files (x86)\Java\jdk1.6.0_26\bin

    Only when I deleted the other two, javac compiler started working, and I could execute HelloWorld.class afterwards.

    • #2 by catalin.boja on July 26th, 2011

      Hi Ike,

      Thank you for reminding me about the restart. I have updated the post.
      What do you mean about deleting the other 2 lines ? These two JAVA_HOME and CLASSPATH.
      If this is the case, be aware that for more complex examples than HelloWorld you need classes from the Java framework which are placed in the % JAVA_HOME%\jre\lib.
      You can skip JAVA_HOME variable because it is just an alias for the full path of the Java folder. In the CLASSPATH variable just replace %JAVA_HOME% with the path.

    • #3 by sesha on June 7th, 2012

      superb it works on windows 7

    • #4 by sesha on June 7th, 2012

      no need to ree start on windows 7 it works for me

  2. #5 by sandeep kumar on October 17th, 2011

    Nicely explained. But to reflect all above changes restart is a must. You also need to set JAVA_HOME along with PATH and CLASSPATH.

    For more refer to this link

    http://www.a2ztechguide.com/2011/10/setting-environment-variables-javahome.html

  3. #6 by James on November 9th, 2011

    Thx! Works perfectly!
    Greetings from Chile.

  4. #7 by Du Tu Van on March 30th, 2012

    Thank you

  5. #8 by Gary G on April 2nd, 2012

    just add the path to the bin directory in the java folder separated by “;” e.g. C:\Program Files (x86)\Java\jre6\bin and then restart the system.

  6. #9 by Flag on April 12th, 2012

    thanks ;)

  7. #10 by rye on May 21st, 2012

    Very well explained explained…..
    Cheers

  8. #11 by Vishal on May 31st, 2012

    very thanks friend for this information now i get rid from this problem :) keep rocking thanks again :)

  9. #12 by Matt H on June 22nd, 2012

    Thanks! Worked Great!

  10. #13 by Komal on June 29th, 2012

    Good Article…Works fine!

  11. #14 by deepak on July 10th, 2012

    great its works 100%

    • #15 by Raja on August 10th, 2012

      Thanks it is working perfectly… Thanks a lot

  12. #16 by Tarrin on August 6th, 2012

    After messing with those for an hour and a half trying other solutions this one worked perfectly. Excellent article, thank you so much.

  13. #17 by Shyam on August 16th, 2012

    Yes, it is working fine for Windows7. Thanks

  14. #18 by ragavan on September 24th, 2012

    tanx a lot… :)

  15. #19 by Nikil on September 26th, 2012

    Thanks a lot!! worked perfectly when tryin to install android SDK!! :)

  16. #20 by bhavi on December 11th, 2012

    thank u… it works :)

  17. #21 by WAHEED on March 3rd, 2013

    tnx alot

    Regards waheed

  18. #22 by waseem khan on March 12th, 2013

    thankssssssss………..
    this is successfully working

    • #23 by BENARD on July 1st, 2013

      rye :
      Very well explained explained…..
      Cheers

      WOW, works perfectly

  19. #24 by Azmil on June 22nd, 2013

    Thanks a lot my friend. This site is a lifesaver:)

  20. #25 by chhaya on December 29th, 2013

    after writing the simple Helloworld program and writing java Helloworld in cmd prompt it shows an error: “could not find or load main class “

    • #26 by Catalin on January 6th, 2014

      Hi Chhaya,

      Please verify if the class name is identical with the file name. Java is a case sensitive language.
      Also, have you compiled the .java file withe the javac.exe compiler into the bytecode file, .class ?

(will not be published)