Print Classpath in Java

Print CLASSPATH in java

In this post, we will see how to print CLASSPATH in java.

💡 Outline

You can use System.getProperty("java.class.path") to get ClassPath in java.

What is CLASSPATH in java

CLASSPATH variables tell application to location where it should find user classes.

Default CLASSPATH is current working directory(.). You can override this value using CLASSPATH variables or -cp command.

how To Print ClassPath in Java

You can extract ClassPath from java.class.path using System.getProperty() and split its paths using String’s split() method.
Using System.getProperty()

Output:

C:\Users\Arpit\IdeaProjects\JavaPrograms\target\classes
C:\Users\Arpit\.m2\repository\org\jsoup\jsoup\1.13.1\jsoup-1.13.1.jar
C:\Users\Arpit\.m2\repository\com\opencsv\opencsv\5.3\opencsv-5.3.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-lang3\3.11\commons-lang3-3.11.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-text\1.9\commons-text-1.9.jar
C:\Users\Arpit\.m2\repository\commons-beanutils\commons-beanutils\1.9.4\commons-beanutils-1.9.4.jar
C:\Users\Arpit\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar
C:\Users\Arpit\.m2\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-collections4\4.4\commons-collections4-4.4.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-math3\3.6.1\commons-math3-3.6.1.jar
C:\Users\Arpit\.m2\repository\com\google\guava\guava\14.0.1\guava-14.0.1.jar

how To Print Path of Java Running Program

If you want to print current working directory i.e. directory where program was started, you can print absolute path using new File(".").getAbsolutePath()

Output:

C:\Users\Arpit\IdeaProjects\JavaPrograms\.

As you can see, this program prints current working directory where program was started.

Frequently Asked Questions

1. how To Print CLASSPATH on Windows?

You can print ClassPath on window using echo command as below:

C:> echo %CLASSPATH%

2. how To Print CLASSPATH on Linux?

You can print ClassPath on Linux with echo command as below:

% echo $CLASSPATH

3. What Is Default CLASSPATH in Java?

Default CLASSPATH is current working directory in java.

That’s all about How to print CLASSPATH in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *