How to find out the classpath and where all the jar's are loaded from in java

You are here : Home / Core Java Tutorials /

In this tutorial we will learn program on How to find out the classpath and where all the jar's are loaded from in java.
i.e. find out all the jars in classpath.

Contents of page >
  • Program 1 - Write Program on How to find out the classpath and where all the jar's are loaded from in java >
  • Program 2 - Write Program on How to find out the classpath and where all the jar's are loaded from in java >



Program 1 - Write Program on How to find out the classpath and where all the jar's are loaded from in java >
we will find out the classpath and where all the jar's are loaded from by using System.getProperty("java.class.path").
import java.io.File;
import java.io.IOException;
/**
* Program on How to find out the classpath and where all the jar's are loaded from in java
*
*/
public class FindClasspathAndWhereAllJjarAreLoadedFrom {
   public static void main(String[] args) throws IOException {
  
          /*
          * we will find out the classpath and where all the jar's are loaded from by using System.getProperty("java.class.path").
          */
          String classpath = System.getProperty("java.class.path");
          // Get all class path enteries in a string array
          String[] classpathEntries = classpath.split(File.pathSeparator);
          System.out.println("CLASS PATH ENTERIES > ");
          for (String classPathEntry : classpathEntries)
                 System.out.println(classPathEntry);
      
   }
}
/*OUTPUT
CLASS PATH ENTERIES >
E:\workspace\FileHandling\bin
E:\java jars\commons\commons-io-1.3.2.jar
*/
Output of program may vary on different systems depending on jars configured in classpath.


Program 2 - Write Program on How to find out the classpath and where all the jar's are loaded from in java >

we will find out the classpath and where all the jar's are loaded from by getting urls from  classLoader.
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* Program on How to find out the classpath and where all the jar's are loaded from in java
*
*/
public class FindClasspathAndWhereAllJjarAreLoadedFrom2 {
   public static void main(String[] args) throws IOException {
          /*
          * we will find out the classpath and where all the jar's are loaded from by getting urls from  classLoader
          */
          ClassLoader classLoader = ClassLoader.getSystemClassLoader();
          URL[] urls = ((URLClassLoader) classLoader).getURLs();
          for (URL url : urls) {
                 System.out.println(url.getFile());
          }
     
   }
}
/*OUTPUT
/E:/workspace/FileHandling/bin/
/E:/java%20jars/commons/commons-io-1.3.2.jar
*/

Output of program may vary on different systems depending on jars configured in classpath.


Summary -
So in this tutorial we wrote programs on How to find out the classpath and where all the jar's are loaded from in java




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Find out Java version (32 or 64 bit) installed in your system using java program and cmd

how to check string contains substring or not using indexOf and contains method in java


How to find out location from where the main java class you are running is loaded


eEdit
Must read for you :