Java Utililty Methods ClassPath

List of utility methods to do ClassPath

Description

The list of methods to do ClassPath are organized into topic(s).

Method

URLloadFromClasspath(Class clazz, String fileName)
load From Classpath
URL dirURL = clazz.getResource("/" + fileName);
if (dirURL == null)
    return null;
URL url = null;
try {
    String dirPath = dirURL.getPath();
    if (dirURL.getProtocol().equals("jar")) {
        int exclamation = dirPath.indexOf("!");
...
URLloadFromClasspath(final String configFileName)
Returns a URL pointing to the given config file.
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
    return ClassLoader.getSystemResource(configFileName);
} else {
    return cl.getResource(configFileName);
URLloadResourceFromClasspath(Class clazz, String resourceName)
Attempts to load a given resource from the classpath.
URL ret = null;
ret = clazz.getResource(resourceName);
if (ret == null) {
    ret = clazz.getResource("/" + resourceName);
return ret;
URL[]parseClassPath(String classpath)
Parse a Java classpath string to URLs.
final String splitPattern = Character.toString(File.pathSeparatorChar);
final String wildcardPattern = ".+\\*";
String[] paths = classpath.split(splitPattern);
List<URL> urls = new ArrayList<URL>();
for (String path : paths) {
    path = path.trim();
    if (path.matches(wildcardPattern)) {
        File folder = new File(path.replace("\\*", ""));
...
URL[]parseJavaClassPath()
Returns the URLs in the class path specified by the java.class.path System#getProperty system property .
ImmutableList.Builder<URL> urls = ImmutableList.builder();
for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) {
    try {
        try {
            urls.add(new File(entry).toURI().toURL());
        } catch (SecurityException e) { 
            urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
    } catch (MalformedURLException e) {
        AssertionError error = new AssertionError("malformed class path entry: " + entry);
        error.initCause(e);
        throw error;
return urls.build().toArray(new URL[0]);
voidprintClassPath()
print Class Path
System.out.println("========================");
System.out.println("     printClassPath");
System.out.println("========================");
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) cl).getURLs();
for (URL url : urls) {
    System.out.println(url.getFile());
System.out.println("----------------------");
System.out.println("");
System.out.println("");
System.out.println("");
voidprintClassPath(boolean single)
print Class Path
ClassLoader cl = ClassLoader.getSystemClassLoader();
URLClassLoader urlcl = (URLClassLoader) cl;
URL[] urls = urlcl.getURLs();
if (single) {
    String cp = System.getProperty("java.class.path");
    System.out.println(cp);
} else {
    for (URL url : urls) {
...
voidprintJVMClassPath()
print JVM Class Path
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();
for (int i = 0; i < urls.length; i++)
    System.out.println("   - " + urls[i].getFile());
StringreadFileFromClasspath(String fileName)
read File From Classpath
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (url == null)
    throw new IOException("Unable to find file: " + fileName);
return getFileContents(getFilePath(url));
BufferedReaderreadFromClassPath(String filePath)
read From Class Path
String path = filePath;
System.err.println("!!!reading config!!!");
System.err.println("from : " + filePath);
StringBuffer fileData = new StringBuffer(1000);
URL res = Object.class.getResource(path);
while (res == null && path.contains(".")) {
    path = path.replaceFirst("\\.", Matcher.quoteReplacement(File.separator));
    res = Object.class.getResource(path);
...