Back

Explore Courses Blog Tutorials Interview Questions
+15 votes
9 views
in Java by (800 points)

What exactly is classpath, what value does it hold ?

Can you also tell me the command that can be used to set classpath ?

2 Answers

+11 votes
by (13.2k points)
edited by

CLASSPATH is actually an environment variable in Java, and tells Java applications and the Java Virtual Machine (JVM) where to find the libraries of classes. These include any that you have developed on your own. An environment variable is a global system variable, accessible by the computer's operating system (e.g., Windows). Other variables include COMPUTERNAME and USERNAME. In Java, CLASSPATH holds the list of Java class file directories and the JAR file, which is Java's delivered class library file.
To set CLASSPATH, there are two ways you can follow
1. Using Command Prompt
2. Using System Settings ( for windows only )
 
USING COMMAND PROMPT :

Command to set CLASSPATH in windows :

If there is no CLASSPATH already existing then we can use below command.

   SET CLASSPATH=.;C:\Program Files\Java\jdk1.7.0_75\lib


A thing to be aware of is we need to have a ‘.;’ before our actual path to denote the current directory.
If a CLASSPATH entry already exist then we can use the command below,

    SET CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib


Command to set CLASSPATH in Unix/Linux

    export CLASSPATH="classpath"


Now, either .bash_profile or .bashrc script can be used which will run whenever you login into your Linux or Unix Machine. To check the value of classpath, echo command can be used.


USING SYSTEM SETTINGS :


To set Classpath,
 1. Go to system properties ( search properties in your machine, using the start key [the left bottom most option on your desktop])
 2. Select Advance Settings.
 3. Choose environment variables and search for System Variables tab.
 4. In System variables, Click Classpath and edit.
 5. If Classpath doesn't exist, then select new.( You will have to specify the variable name in this case, to denote current directory use dot (.))
 6. Add all folders separated with separator.
 7. Click OK.

0 votes
by (40.7k points)

The classpath is the path where the JVM i.e. Java Virtual Machine looks for user-defined classes, packages, and resources in Java programs.

Sometimes, you can use the format() method to load a template file from this path.

Want to learn Java from scratch! Go through this scratch to advance video on JAVA:

by (110 points)
This video is really useful

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 27, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

Browse Categories

...