Java Class Path getClassBasePath(Class aClass)

Here you can find the source of getClassBasePath(Class aClass)

Description

get Class Base Path

License

Open Source License

Declaration

public static File getClassBasePath(Class aClass) 

Method Source Code


//package com.java2s;
/*//from  w ww .java 2s .co  m
 * Copyright (C) 2002-2006 Stefan Stiller
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;
import java.io.UnsupportedEncodingException;

import java.net.URL;
import java.net.URLDecoder;

public class Main {
    public static File getClassBasePath(Class aClass) {
        String className = aClass.getName();
        className = className.replace('.', '/') + ".class";
        URL url = aClass.getClassLoader().getResource(className);
        if ("file".equals(url.getProtocol())) {
            String dir = url.getFile();
            dir = dir.substring(0, dir.length() - className.length());
            try {
                dir = URLDecoder.decode(dir, "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
            return new File(dir);
        }
        if ("jar".equals(url.getProtocol())) {
            String dir = url.getFile();
            if (dir.startsWith("file:/")) {
                String jarFile = dir.substring(6, dir.indexOf('!'));
                try {
                    jarFile = URLDecoder.decode(jarFile, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                }
                return new File(jarFile);
            }
        }
        throw new RuntimeException("Couldn't locate base directory.");
    }

    public static boolean equals(Object o1, Object o2) {
        if (o1 == o2)
            return true;
        else if (o1 == null)
            return false; // o2 is not null because of the first test
        else
            return o1.equals(o2);
    }
}

Related

  1. getAbsolutePath(final Class clazz, final String relative)
  2. getApplicationPath(Class cls)
  3. getAppPath(Class clazz)
  4. getClass(String parentPath, String className)
  5. getClassAbsolutePath(Class c)
  6. getClassFilePath(Class clazz)
  7. getClassFilePath(Class clazz)
  8. getClassFilePath(Class clazz)
  9. getClassFilePath(final Class cls)