简体   繁体   中英

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?

Unless you explicitly set the CLASSPATH variable in either the current shell or in your login profile (eg ~/.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.

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"));

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM