Basil Bourque
Basil Bourque

Reputation: 338171

Print the Java Classpath on macOS (Mac OS X)

On the command-line, how can I see all the folders and files comprising my current Java classpath?

The Oracle Tutorial says to use echo $CLASSPATH on Unix-like systems. But on a Mac (El Capitan) with Java 8 that prints nothing. As I recall, by default the CLASSPATH environment variable is not used on macOS.

I saw some Questions such as this that show setting the classpath, but I just want to verify the classpath.

Is there some easy way from the command-line to examine the current classpath?

Upvotes: 8

Views: 20181

Answers (4)

learningIsFun
learningIsFun

Reputation: 152

In Java Code, using below helps to differentiate easily: System.out.println(System.getProperty("java.class.path")); System.out.println(System.getProperty("sun.boot.class.path"));

Upvotes: 3

Lv99Zubat
Lv99Zubat

Reputation: 853

In your java code do:

System.out.println(System.class.path);

This lists all the classpaths to the jar files (java platform classes), the class path for your project, etc. The classpaths are separated by semicolons. Michael Markidis is right about "If you don't have the CLASSPATH set, then the default value of the class path is ".", meaning that only the current directory is searched." And you should find that in this list.

When classes are stored in a directory (folder), such as c:\java\MyClasses\utility\myapp, then the class path entry points to the directory that contains the first element of the package name (in this case, C:\java\MyClasses, because the package name is utility.myapp).

So if you use packages, the ending directory of your applications class path should be your root package.

Upvotes: 1

Michael Markidis
Michael Markidis

Reputation: 4191

Unless you explicitly set the CLASSPATH variable in either the current shell or in your login profile (e.g. ~/.bash_profile), then there is no way to show what it is.

If you don't have the CLASSPATH set, then the default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

Upvotes: 4

Rae Burawes
Rae Burawes

Reputation: 912

I'm not sure if $CLASSPATH is available in OSX by default. But, $PATH might help you. This variable has the information about the directories that contain executable commands.

Upvotes: 3

Related Questions