|
Java Virtual Machine(JVM) uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error (java.lang.ClassNotFoundException). error caused while JVM is interpreting the code Ways to Set a ClasspathThere are five different ways to set a classpath. These are:
The limitation of –cp,–classpath, and –class-path methods is that it can set classpath only for the current command line, in the next command line, if we access the desired class directly, we will get an Exception Syntax: >java -cp <directory_location> <class name> Example: Java
how to set classpath by using -cp command Command Line Settings –>java -cp <directory_location> <class name> OR >java -classpath <directory_location> <class name> OR >java --class-path <directory_location> <class name>
Temporary Settings:>set classpath=<directory_location> the temporary setting of classpath using ‘set classpath’ command If we want to have classpath settings permanently for all the command prompts, we must set classpath in the environment variable section. Permanent Settings:
Environment Variables Setting permanent classpath settings We must include .; in the classpath beginning so that the JVM can access both the current working directory and the directory of the desired class file, respectively. >java -cp <.;directory_location> <class name> Importance of ‘.’ in the classpathIf we set classpath pointing to one directory, if we don’t place (dot), the classes available in the current working directory will not be found by compiler and JVM. ‘.’ represents the current working directory. The current working directory means not the folder where you save the .java file, the folder path where you open the command prompt. Syntax to include multiple paths in the classpathWe must separate each folder location with a semicolon separator. java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass
Difference between path and classpath searching algorithm priority
|
Reffered: https://www.geeksforgeeks.org
Java |
Related |
---|
![]() |
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |