Java - Understanding classpath
by
Jeremy Canfield |
Updated: March 09 2023
| Java articles
JAR files contain classes, perhaps something like this.
com/example/foo.class
com/example/bar.class
Let's say the JAR file is located at
/usr/local/jars/example.jar
In this scenario, if a Java application wants to use foo.class and bar.class in example.jar, the Java application will need to include /usr/local/jars in it's classpath. For example, if you have a Java application that needs to use foo.class or bar.class in example.jar, the -classpath option can be used to include the .jar files in a directory, like this.
/usr/bin/java -classpath /usr/local/jars myapp
On Linux, this command will temporarily update the CLASSPATH variable to contain a certain directory.
CLASSPATH=/opt/jars
The echo command can be used to verify that CLASSPATH now contains /opt/jars.
echo $CLASSPATH
Did you find this article helpful?
If so, consider buying me a coffee over at