What are the differences between path and classpath in java?

Learn: What are the differences between path and classpath variable in java? Explain in brief path and classpath variable in java? By Preeti Jain Last updated : January 02, 2024

PATH (path) in Java

  • path is an environment variable.
  • path describes the location(or place) where javac.exe (binary executables file) is located.
  • So whenever we try to compile and run a java program just before we have to set path variable of jdk\bin to run or compile a java program.

Setting path in Java

D:\Java Articles> set path=C:\Program Files\Java\jdk1.8.0_111\bin;

Checking path is properly set or not

D:\Java Articles> javac Usage: javac <options> <source_files>

Where, possible options include:

  • -g: Generate all debugging info,
  • -g:none: Generate no debugging info

What kind of error we will get if path is not set properly?

D:\Java Articles> javac

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

We will see what will happen if we don't set path variable or set wrong path of jdk\bin folder just before compiling or executing a java program. javac won't work.

Example

D:\Java Articles> javac

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

javac.exe file will be available in jdk\bin.

CLASSPATH (classpath) in Java

  • classpath is also an environment variable.
  • classpath describes the location where required (.class) file is located.

Setting classpath variable in Java

D:\Java Articles > set classpath = C:\Program Files\Java\jre1.8.0_111\lib;

Checking classpath variable set or not (Run java file)

1) If path sets properly then we will get output.

D:\Java Articles> java Classpath
welcome in java world

2) If path does not sets properly then we will get error (To run java program)

D:\Java Articles>java Classpath
Error: Could not find or load main class Classpath

3) Java compiler and JVM use classpath.

4) We will see what will happen if we don't set classpath variable or set wrong classpath of .class file then we may not compile and we may not run so both cases may arise sometimes it may not compile or sometime it may not run.

D:\Java Articles>java Classpath
Error: Could not find or load main class Classpath

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.