Saturday, 29 August 2015

Get current project classpath

You can use SystemClassLoader class to get classpath of project. Following application print  the search path of URLs for loading classes and resources.
import java.net.URL;
import java.net.URLClassLoader;

public class ClassPathUtil {
 public static void main(String args[]) {
  ClassLoader loader = ClassLoader.getSystemClassLoader();

  URL[] urls = ((URLClassLoader) loader).getURLs();

  for (URL url : urls) {
   System.out.println(url.getFile());
  }
 }
}


Another simple way is use the environment variable ‘java.class.path’. ‘java.class.path’ is used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.

public class ClassPathUtil {
 public static void main(String args[]) {
  String classpath = System.getProperty("java.class.path");
  System.out.println(classpath);
 }
}


No comments:

Post a Comment