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

StringreadStringFromClasspath(String path, Class c)
Reads the entire InputStream and returns its content as a single String
final InputStream is = c.getResourceAsStream(path);
return readStringFromBufferedReader(createBufferedUtf8Reader(is));
ClassLoadertoClassLoaderWithDefaultParent(Collection classPaths)
to Class Loader With Default Parent
Set<URL> dependencies = new HashSet<URL>();
for (String classPath : classPaths) {
    File dependency = new File(classPath);
    dependencies.add(dependency.toURI().toURL());
ClassLoader classLoader = URLClassLoader.newInstance(dependencies.toArray(new URL[dependencies.size()]));
return classLoader;
StringtoClasspathString(ClassLoader cl)
to Classpath String
if (cl == null) {
    cl = Thread.currentThread().getContextClassLoader();
StringBuilder back = new StringBuilder();
while (cl != null) {
    if (cl instanceof URLClassLoader) {
        URLClassLoader ucl = (URLClassLoader) cl;
        URL[] urls = ucl.getURLs();
...
File[]toFiles(String classPath)
to Files
String[] paths = classPath.split(File.pathSeparator);
File[] files = new File[paths.length];
for (int i = 0; i < paths.length; i++) {
    files[i] = new File(paths[i]);
    if (!files[i].exists()) {
        throw new IllegalArgumentException("Unknown path:" + paths[i]);
return files;
URL[]tryToGetClassPath(ClassLoader cl)
try To Get Class Path
if (cl instanceof URLClassLoader) {
    return ((URLClassLoader) cl).getURLs();
return new URL[0];