List of utility methods to do ClassPath Get
URL | getClasspathResource(final String resource) get Classpath Resource return Thread.currentThread().getContextClassLoader().getResource(resource);
|
URL | getClasspathResource(String name) Finds the class path resource with the given name and returns it as a URL. return Thread.currentThread().getContextClassLoader().getResource(name);
|
InputStream | getClasspathResource(String path) get Classpath Resource if (path == null) { throw new IllegalArgumentException("the parameter 'path' is required."); URL url = getClasspathResourceURL(path); if (url == null) { throw new IllegalArgumentException("the resource path[" + path + "] could not be found."); InputStream inStream; ... |
File | getClasspathResourceAsFile(String resourceName) get Classpath Resource As File return new File( new URI(Thread.currentThread().getContextClassLoader().getResource(resourceName).toString())); |
List | getClassPathResourcesAsStreams(String relativeResourceName) Search resources within the classpath that match the given name, create input streams from the found resources and return them as a list. List<InputStream> result = new ArrayList<InputStream>(); try { Enumeration<URL> resourceUrls = Thread.currentThread().getContextClassLoader() .getResources(relativeResourceName); while (resourceUrls.hasMoreElements()) { result.add(((InputStream) (resourceUrls.nextElement().getContent()))); } catch (IOException e) { ... |
File | getClassPathRoot(Class> cl) get Class Path Root try { String resourceName = translateResourceName(cl); URL url = getClassURL(cl.getClassLoader(), resourceName); if (url.getProtocol().equals("file")) { String fileName = URLDecoder.decode(url.getPath()); String rootDirName = fileName.replaceAll(resourceName + "$", ""); return new File(rootDirName); } else { ... |
String | getClasspathString() get Classpath String ClassLoader cl = Optional.ofNullable(Thread.currentThread().getContextClassLoader()) .orElse(ClassLoader.getSystemClassLoader()); while (!URLClassLoader.class.isInstance(cl)) { cl = cl.getParent(); if (cl == null) return "Classpath is empty"; return " " + Stream.of(cl).map(URLClassLoader.class::cast).flatMap(c -> Arrays.stream(c.getURLs())) ... |