Java Jar File Find getJarFolder(Class clazz)

Here you can find the source of getJarFolder(Class clazz)

Description

get Jar Folder

License

Open Source License

Declaration

public static File getJarFolder(Class clazz) 

Method Source Code


//package com.java2s;
import java.io.File;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static File getJarFolder(Class clazz) {
        File currentJar = urlToFile(clazz.getProtectionDomain().getCodeSource().getLocation());

        return currentJar;

        // Old broken way (java.net.URL is really a total mess)

        // String name = StaticHelper.class.getName().replace('.', '/');
        // URL url = StaticHelper.class.getResource("/" + name + ".class");
        // String s="";
        ///*  www  . jav  a  2s  .c o m*/
        // // we have to do the following hack because the java.net.url class is
        // // broken and there is no new File(URI uri) constructor.
        // // So we have to account for broken urls and whitespace chars being
        // // converted to %20 and stuff like that. A file cannot be created
        // // out of a url path containing whitespace chars in encoded form.
        // // even url.toUri() cannot repair this mess.
        //
        // try {
        // s = URLDecoder.decode(url.toExternalForm(), "UTF-8");
        // } catch (UnsupportedEncodingException e1) {
        // // TODO Auto-generated catch block
        // e1.printStackTrace();
        // }
        //
        // // Class is not in Jar
        // if (s.lastIndexOf(".jar") == -1) {
        // return null;
        // }
        // //
        // jar:file:/home/frank/PDGFEnvironment/pdgf.jar!/pdgf/util/StaticHelper.class
        //
        // s = s.replace('/', File.separatorChar);
        // s = s.substring(0, s.indexOf(".jar") + 4);
        // int idx = s.lastIndexOf("jar:file:");
        // if (idx > -1) {
        // s = s.substring(idx + "jar:file:".length());
        // }
        // idx = s.lastIndexOf("jar:");
        // if (idx > -1) {
        // s = s.substring(idx + "jar".length());
        // }
        //
        // // s = s.substring(s.lastIndexOf(':') - 1);
        // return s.substring(0, s.lastIndexOf(File.separatorChar) + 1);
    }

    /**
     * code from:
     * http://blogs.sphinx.at/java/erzeugen-von-javaiofile-aus-javaneturl/
     * 
     * @param url
     * @return
     */
    public static File urlToFile(URL url) {
        URI uri;
        try {
            uri = url.toURI();
        } catch (URISyntaxException e) {
            // obviously the URL did
            // not comply with RFC 2396. This can only
            // happen if we have illegal unescaped characters.

            try {
                uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                        url.getQuery(), url.getRef());
            } catch (URISyntaxException e1) {
                // The URL is broken beyond automatic repair
                throw new IllegalArgumentException("broken URL: " + url);
            }
        }
        return new File(uri);
    }
}

Related

  1. getJarFile(Class clazz)
  2. getJarFile(Class clazz)
  3. getJarFile(final ClassLoader loader)
  4. getJarFile(String classPath)
  5. getJarFileByResourceName(Class clazz, String resourceName)
  6. getJarLocation()
  7. getJarLocation(Class classFromJar)
  8. getJarLocation(Class clazz)
  9. getJarLocation(final Class cClass)