Overview
To Set Java Classpath, the classpath in Java is a crucial concept, as it tells the Java Virtual Machine (JVM) and Java compiler where to find class files and packages. If you’re developing Java applications, you’ll often need to set the classpath to ensure that your programs run correctly. This blog will guide you on how to Set Java Classpath in various environments.

What is the Java Classpath?
The classpath is an environment variable or command-line option that specifies the directories or JAR files that contain the compiled Java classes needed by your application.
Without the correct classpath, Java won’t be able to find the necessary class files, and you’ll encounter ClassNotFoundException
errors.
As a Java beginner after installing JDK, and before writing the Hello World program the challenge is how to set Java Classpath and path, so the program gets compiled and execute it.
Before talking about setting path, lets understand about PATH and CLASSPATH, Why we need JAVA_HOME, PATH and CLASSPATH?
- JAVA_HOME – Java installation directory/location called JAVA_HOME, it is required during the compile and runtime of the java program. If your application is Maven or Gradle base application, it always use the JAVA_HOME to compile the application and download the supporting dependencies.
- PATH -You need to set PATH to compile Java source code, create JAVA CLASS FILES and Operating System to load classes at runtime.
- CLASSPATH – The classpath is required during run time of class, JVM load classpath libraries and execute the java program.
There are couple of way to set the java/jdk path, lets understand it.
Before set the JDK Java class path if you execute any jdk command such javac, java it will get below.
C:\>javac Hello.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
Setting JDK path in Windows:
1) By Windows environment properties –
- Goto the environment properties and open the environment properties for your user account.

- Set the JAVA_HOME path which is installation directory of jdk, for example – C:\HOMEWARE\ITToolbox\apps\Java\jdk-17.0.7_windows-x64_bin
- Edit the environment variable under the path, add new path %JAVA_HOME%\bin or C:\HOMEWARE\ITToolbox\apps\Java\jdk-17.0.7_windows-x64_bin\bin, where %JAVA_HOME% refer to JAVA_HOME which we have set in previous step. Then click the OK.
- Now after setting the path verify, it in command prompt. if you find the below screen then the JDK path is set on your system. This path can be used for all the process.
2) Also, the java class path can be set by using command prompt. but this can be only for particular Powercell session and verify using javac command.
c:\>SET PATH=%PATH%;C:\apps\Java\jdk-17.0.7_windows-x64_bin
c:\>javac -version
javac 17.0.7
Setting Java Path in Linux OS
Setting path in Linux OS is the same as setting the path in the Windows OS. But, here we use the export tool rather than set. Let’s see how to set Java class path in Linux OS.
export JAVA_HOME=/home/jdk-17.0.7_windows-x64
export PATH=$PATH:/home/jdk-17.0.7_windows-x64/bin
Hope the above explanation about java class path setup are useful.
Best Practices for Setting Classpath
- Use Relative Paths: If you plan to share your project with others, use relative paths in your project so it works regardless of directory structure.
- Prefer IDEs: If you’re working in an integrated development environment (IDE), let the IDE manage your classpath. It simplifies the process and reduces human error.
- Use JAR Files: When working with third-party libraries, package them into JAR files and include those in your set java classpath, instead of dealing with individual class files.
Conclusion
Setting the Java classpath is essential for running Java programs, especially when working with external libraries and packages. You can set it temporarily in the command line or permanently using environment variables, depending on your needs. Proper classpath management is crucial to avoid runtime errors and ensure your Java application finds all the required classes. To start basics of java refer first Hello World! program.
Happy coding!