Java Class Path Create getClassBasePath(URL classUrl, Class clazz)

Here you can find the source of getClassBasePath(URL classUrl, Class clazz)

Description

get Class Base Path

License

Apache License

Declaration

public static String getClassBasePath(URL classUrl, Class<?> clazz) 

Method Source Code

//package com.java2s;
/**/*  ww  w . j a v  a2  s .c om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Modified by the WebMapReduce developers.
 */

import java.net.*;

public class Main {
    public static String getClassBasePath(URL classUrl, Class<?> clazz) {
        return getClassBasePath(classUrl, clazz.getName());
    }

    public static String getClassBasePath(URL classUrl, String className) {
        String codePath;
        if (classUrl != null) {
            boolean inJar = classUrl.getProtocol().equals("jar");
            codePath = classUrl.toString();
            if (codePath.startsWith("jar:")) {
                codePath = codePath.substring("jar:".length());
            }
            if (codePath.startsWith("file:")) { // can have both
                codePath = codePath.substring("file:".length());
            }
            if (inJar) {
                // A jar spec: remove class suffix in /path/my.jar!/package/Class
                int bang = codePath.lastIndexOf('!');
                codePath = codePath.substring(0, bang);
            } else {
                // A class spec: remove the /my/package/Class.class portion
                int pos = codePath.lastIndexOf(classNameToPath(className));
                if (pos == -1) {
                    throw new IllegalArgumentException(
                            "invalid codePath: className=" + className + " codePath=" + codePath);
                }
                codePath = codePath.substring(0, pos);
            }
        } else {
            codePath = null;
        }
        return codePath;
    }

    public static String classNameToPath(String className) {
        String relPath = className;
        relPath = relPath.replace('.', '/');
        relPath += ".class";
        return relPath;
    }
}

Related

  1. getClassPathArchiveContents(final URL resource)
  2. getClasspathResourceURL(String resourceName)
  3. getClasspathUrl(final Class aClass, final String aPath)
  4. getClasspathURL(final String name)