Java Class Loader getClassloaderRootDir(Class forClass)

Here you can find the source of getClassloaderRootDir(Class forClass)

Description

Provides a binaries root directory.

License

Open Source License

Parameter

Parameter Description
forClass The class used to get the responsible ClassLoader .

Return

the binaries root directory.

Declaration

public static File getClassloaderRootDir(Class<?> forClass) 

Method Source Code


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

import java.io.File;
import java.net.URL;

public class Main {
    /**/*w  w w . j av  a2s  .  c o  m*/
     * Provides a binaries root directory. E.g. a 'target/classes' directory.
     * <p>
     * Does only work well if the classes located in a directory structure.
     *
     * @param forClass The class used to get the responsible {@link ClassLoader}.
     * @return the binaries root directory.
     */
    public static File getClassloaderRootDir(Class<?> forClass) {
        URL rootUrl = forClass.getClassLoader().getResource(".");
        return new File(rootUrl.getFile());
    }
}

Related

  1. getClassLoader()
  2. getClassLoader(File[] directories)
  3. getClassLoader(String path)
  4. getClassLoaderForClass(final Class clazz)
  5. getClassLoaderForDirectory(final File baseFolder)
  6. getClassLoaders(ClassLoader baseClassLoader)
  7. getClassLoaderStack(ClassLoader cl)
  8. getClassLocation(Class clazz)
  9. getClassLocation(String className)