Saturday, February 16, 2013

How to set Java Path and Classpath in Windows 8 and Windows 7 - Tutorial

So, you just bought a new PC or Laptop with Windows 8 operating system, and wondering how to set PATH and Classpath on Windows 8; Or, you might have just upgraded your windows 7 laptop to professional edition of Windows 8 and looking to set JDK Path to compile Java programs. Not to worry, this is second step for any one who wants to learn Java programming. Of course, first step is to install JDK.  In this Java tutorial, we will see step by step guide to set Java PATH and CLASSPATH in Windows 8 operating system. By the way, if you are learning Java in Windows 7 operating system, You should still be able to set Java path and classpath in Windows 7 by following steps given here, because navigation steps for modifying environment variables on Windows 7 and Windows 8 are almost same.

For absolutely Java beginners, It's important to know What is Path Environment variable and Why do you need to set Path for Java? PATH is an environment variable, which contains location of executable or commands. When you type a command in command prompt, Operating System like Windows 7 or Windows 8, looks PATH  to find and execute that command. Actually for running Java program, you don't need to set PATH because Windows 8 comes with bundled JRE which is sufficient for running Java applications. Setting PATH simply means including JDK bin directory into PATH environment variable. You need to set PATH to compile Java source code and create Java class files.  JDK bin directory contains javac and java  commands to compile and run Java programs. CLASSPATH is little different than Path and it's used by JVM rather than Operating System to load classes at runtime. I suggest reading difference between Path and Classpath and How Classpath works in Java and to know more about these fundamental concept.

Setting JDK Path and Classpath in Windows 8 and Windows 7

This tutorial assumes that, you know how to download and Install JDK in Windows 8 machine. By the way if you are absolutely beginner and just starting with Java, then it's important to download correct version of JDK based upon whether you are running 32-bit or 64-bit version of Windows 8. See How to install JDK 7 on Windows 8 operating System to learn more about installing JDK in Windows 7 and 8. Once successfully installed, it's time to set PATH and Classpath for Java. By the way, If you know how to set PATH in Windows XP, then this would be very easy for you.

Steps to set JDK Path and Classpath in Windows 7 and Windows 8

Before setting JDK Path, let's first verify Type javac in command prompt in windows 8 and see output, if you get javac is not recognized as an internal or external command, means JDK Path is not set.

1. Confirm that PATH is not set for Java by typing javac in command prompt.
2. Open Control Panel and Select System and Security
3. Select System
4. Select Advanced System Settings
5. Select Environment Variables
6. Select and Edit Path Environment variable
7. Verify Java path by typing javac in command prompt

Step 1 : Open Control Panel in Windows 8
How to set PATH in Windows 8 operating System

Step 2 : Select System
How to set Classpath in Windows 8

Step 3 : Select Advanced System Settings in Windows 8
How to set path in Windows 7

Step 4 : Select Environment Variables
How to set Classpath in Windows 7 operating system

Step 5 : Select and Edit Path Environment variable in Windows 8
set path environment variable in windows 8 operating system

Step 6 : Verify Java path by typing javac in command prompt
just type javac command, this time you will see different output which shows various details of java command rather than getting earlier error.You can follow similar process to set Classpath in windows 8 as well. But it’s not a good practice to use CLASSPATH environment variable to run Java application, instead every Java application should use –classpath option to specify classpath. You can also see How Classpath works in Java for more details on Java Classpath.  

Now you are ready to write Java code and compile and run them. Why not just try with HelloWorld Java program. By the way to know more about PATH and Classpath, which is essential for Java developers, See following tutorials


Anonymous said...

i lost my predefined path for windows8.. instead of appending it i placed new java path and it does not work.. pls let me know if u kno how to make it work!!

Anonymous said...

I did everything as it said, but I still get the error message when I type in javac. Any ideas what I did wrong?

Javin @ puzzle asked in programming interviews said...

@Anonymous, please provide some more details and I can try to help you. I know this PATH Setting can be tricky some time.

Anonymous said...

After working on the path as sa, I typed in java on the cmd window and it worked, but javac still doesn't.
This is my path value as at now:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\PharosSystems\Core\;C:\Program Files\Java\jre7\bin

Anonymous said...

hi, how can I get back my pre-defined path? I inadvertently deleted...:(


Anonymous said...

I have the same person as the May 6 post. My PATH variable ends C:\Program Files (x86)\Java\jre7;C:\Program Files (x86)\Java\jdk\bin

I type "java" in the cmd prompt and it words: I type "javac" and I get the error message' "'javac' is not recognized...".

Any ideas? Thanks

Javin @ Connection refused exception in Java said...

Hi Anonymous, Can you double check if C:\Program Files (x86)\Java\jdk\bin directory contains "javac" command or not?

Joseph Don said...

Does typing javac in the command prompt set the path automatically?

Kevin Clifford said...

It gives me this error when i run a .bat file to run my java program: "D:\Videogames\Minecraft\server>java -Xmx1024M -jar
craftbukkit.jar -o true
'java' is not recognized as an internal or external command, operable program or batch file."

Press any key to continue..."

How do I fix this?

Anonymous said...

I am also got problem with windows 8, I delete the whole path in environment variables could you please send the command prompt path to my email ( ) thanks!!!

How to Set Path & Classpath in Windows 8 for Environmental Variables and Command Prompt. said...

Nice Tutorial, for the new Os Windows 8. The Way you describing with the diagram is excellent. Thanks A lot. Representing the Windows 8 Setting Class path for the Java JDK in Diagrammatic Form. its Nice.
For more Detail Explanation for the Setting Class path for Java JDK in windows 8 Step by step

Siva Balaji said...

By mistaken I deleted my predefined path for windows8.. instead of appending it i placed new java path and it does not work..
Please give me a help to recover this problem

Anonymous said...

By the way, you can get to the point earlier.

Anonymous said...

Hello friends !

To set up path for java in windows 8, there are to box is one is user variable and second one is system variable, so from system variable(below box) find "Path" and select that and than click on "Edit" button. Type a Simi colon(;) at the end of the line and also do not delete line, and than past complete path of java installation folder for example :C:\Program Files\Java\jdk1.7.0_67\bin: and than click Ok.
Hope it will work for you

Post a Comment