Java ClassLoader Load getBaseDir()

Here you can find the source of getBaseDir()

Description

Gets the application base directory.

License

Open Source License

Return

the base directory

Declaration

public static URL getBaseDir() 

Method Source Code


//package com.java2s;
/*/*from  w w w.jav  a2s  . c om*/
 * SCUBA smart card framework.
 *
 * Copyright (C) 2009 - 2012  The SCUBA team.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $Id: $
 */

import java.io.File;

import java.net.URL;

public class Main {
    /**
     * Gets the application base directory.
     * 
     * @return the base directory
     */
    public static URL getBaseDir() {
        try {
            Class<?> c = (new Object() {
                public String toString() {
                    return super.toString();
                }
            }).getClass();

            /* We're using the classloader to determine the base URL. */
            ClassLoader cl = c.getClassLoader();
            URL url = cl.getResource(".");

            if (url == null) {
                return getBaseDir(c);
            }

            String protocol = url.getProtocol().toLowerCase();
            String host = url.getHost().toLowerCase();
            String basePathString = url.getFile();

            if (basePathString.endsWith("/bin") || basePathString.endsWith("/bin/")
                    || basePathString.endsWith("/build") || basePathString.endsWith("/build/")) {
                basePathString = (new File(basePathString)).getParent();
            }
            return new URL(protocol, host, basePathString);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Gets the base directory where class <code>c</code> resides.
     *
     * @param c a class
     * 
     * @return the base directory
     */
    public static URL getBaseDir(Class<?> c) {
        try {
            String className = c.getCanonicalName();
            if (className == null) {
                className = c.getName();
            }
            String pathToClass = "/" + className.replace(".", "/") + ".class";
            URL url = c.getResource(pathToClass);

            String protocol = url.getProtocol().toLowerCase();
            String host = url.getHost().toLowerCase();
            String dirString = url.getFile();
            int classNameIndex = dirString.indexOf(pathToClass);
            String basePathString = dirString.substring(0, classNameIndex);
            if (basePathString.endsWith("/bin") || basePathString.endsWith("/bin/")
                    || basePathString.endsWith("/build") || basePathString.endsWith("/build/")) {
                basePathString = (new File(basePathString)).getParent();
            }
            URL basePathURL = new URL(protocol, host, basePathString);
            return basePathURL;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getAbsoluteBasePath()
  2. getAbsolutePath(String file)
  3. getAbsolutePathEssenceDesc(String file)
  4. getAllArquillianLibs()
  5. getAsProperties(final String name)
  6. getBytes(File file)
  7. getConfigFileInputStream(String configFilePath)
  8. getContentFromFile(String fileName, Object context)
  9. getDirectories(String path)