Java ClassPath Get getClasspathString()

Here you can find the source of getClasspathString()

Description

get Classpath String

License

Open Source License

Declaration

private static String getClasspathString() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    private static String getClasspathString() {
        ClassLoader cl = Optional.ofNullable(Thread.currentThread().getContextClassLoader())
                .orElse(ClassLoader.getSystemClassLoader());

        while (!URLClassLoader.class.isInstance(cl)) {
            cl = cl.getParent();/*w ww.  java  2  s. c o  m*/
            if (cl == null)
                return "Classpath is empty";
        }

        return "  " + Stream.of(cl).map(URLClassLoader.class::cast).flatMap(c -> Arrays.stream(c.getURLs()))
                .map(URL::getFile).collect(Collectors.joining("\n  ")) + "\n";

    }
}

Related

  1. getClasspathResource(String name)
  2. getClasspathResource(String path)
  3. getClasspathResourceAsFile(String resourceName)
  4. getClassPathResourcesAsStreams(String relativeResourceName)
  5. getClassPathRoot(Class cl)