Java – How to set classpath in Windows

Learn to set CLASSPATH environment variable for Java in Windows OS. The steps are more or less same in all version of Windows i.e. Windows 7, Windows 8 or Windows 10.

The name of CLASSPATH variable is case-insensitive i.e. classpath and CLASSPATH – both can be used.

1. Steps to set classpath in Windows

  1. Open environment variable window. In Windows 10, we can directly search “environment variable” from taskbar search icon. In older versions, we can navigate by keyboard shortcuts:

    "Windows + Pause" –> Advanced System Settings –> Environment Variables

    Windows + Pause
    Windows + Pause
    Advanced System Settings
    Advanced System Settings

    Environment Variables
    Environment Variables
  2. Create a new variable with name CLASSPATH and set the value to JAVA_HOME\lib. Also, it is best to include current directory by including (dot or period sign).

    The CLASSPATH value is set. We can always edit its value to include or exclude more directories in it.

  3. To verify the classpath changes, open a new command prompt and run command:
    echo %CLASSPATH%

    This command will show the value set for classpath variable.

2. Set temporary classpath from DOS prompt

To set the classpath temporarily from DOS prompt, use the command set CLASSPATH.

set CLASSPATH=.;C:/Program Files/Java/jdk1.7.0_79/lib;

The classpath variable set in this way is not persistent. The classpath will be available only in the current DOS window. It will not be available in any other window or application opened in new command prompts.

Drop me your question in comments.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.