Table of Contents
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.
12345 String classpath = System.getProperty("java.class.path");String[] classPathValues = classpath.split(File.pathSeparator);System.out.println(Arrays.toString(classPathValues));
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.io.File; public class PrintClassPathUsingSystemGetPropery { public static void main(String[] args) { String classpath = System.getProperty("java.class.path"); String[] classPathValues = classpath.split(File.pathSeparator); for (String classPath: classPathValues) { System.out.println(classPath); } } } |
Output:
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.io.File; public class PrintCurrentWorkingDirectory { public static void main(String[] args) { String absolutePath = new File(".").getAbsolutePath(); System.out.println(absolutePath); } } |
Output:
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:
2. how To Print CLASSPATH on Linux?
You can print ClassPath on Linux with echo command as below:
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.