Java ClassPath Get getClassPath(Class c)

Here you can find the source of getClassPath(Class c)

Description

get Class Path

License

LGPL

Declaration

public static URL getClassPath(Class<?> c) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

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

public class Main {
    public static URL getClassPath(Class<?> c) {
        String classFile = c.getName().replace('.', '/') + ".class";
        URL url = c.getClassLoader().getResource(classFile);
        if (url == null)
            return null;

        if (url.getProtocol().equals("jar")) {
            String urlFile = url.getFile();
            int i = urlFile.indexOf("!");
            if (i > 0) {
                try {
                    URL jarURL = new URL(URLDecoder.decode(urlFile.substring(0, i), "UTF-8"));
                    return jarURL;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }//ww w .  j  ava 2  s  .  c  o  m
            }
        }
        String urlString = url.toString();
        if (urlString.endsWith(classFile)) {
            try {
                return new URL(urlString.substring(0, urlString.length() - classFile.length()));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

Related

  1. getClasspath()
  2. getClassPath()
  3. getClasspath()
  4. getClasspath()
  5. getClasspath()
  6. getClassPath(Class clazz)
  7. getClasspath(ClassLoader classLoader)
  8. getClassPath(ClassLoader loader)
  9. getClasspath(ClassLoader loader)