Java URL Connection jarURLEntryResource(String jarURLString)

Here you can find the source of jarURLEntryResource(String jarURLString)

Description

Lookup a jar URL and return the resource.

License

Open Source License

Parameter

Parameter Description
jarURLString The string containing the jar URL.

Return

The resource, if any.If the spec string does not contain !/, then return null.

Declaration

public static URL jarURLEntryResource(String jarURLString) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.URL;

public class Main {
    /** Lookup a jar URL and return the resource.
        //from   w  w w.j  a v  a 2s  .  c o m
     *  A resource is a file such as a class file or image file that
     *  is found in the classpath.  A jar URL is a URL that refers to
     *  a resource in a jar file.  For example,
     *  <code>file://./foo.jar!/a/b/c.class</code> is a jar URL that
     *  refers to the <code>a/b/c.class</code> resource in
     *  <code>foo.jar</code>.  If this method is called with
     *  <code>file://./foo.jar!/a/b/c.class</code> then it will return
     *  <code>a/b/c.class</code> if <code>a/b/c.class</code> can be
     *  found as a resource in the class loader that loaded this class
     *  (ptolemy.util.ClassUtilities).  If the resource cannot be found,
     *  then an IOException is thrown. If the jarURLString parameter
     *  does not contain <code>!/</code>, then return null.
     *  Note that everything before the <code>!/</code> is removed before
     *  searching the classpath.
     *
     *  <p>This method is necessary because Web Start uses jar URL, and
     *  there are some cases where if we have a jar URL, then we may
     *  need to strip off the jar:<i>url</i>!/ part so that we can
     *  search for the {entry} as a resource.
     *
     *  @param jarURLString The string containing the jar URL.
     *  @return The resource, if any.If the spec string does not
     *  contain <code>!/</code>, then return null.
     *  @exception IOException If this method cannot convert the specification
     *  to a URL.
     *  @see java.net.JarURLConnection
     */
    public static URL jarURLEntryResource(String jarURLString) throws IOException {
        // At first glance, it would appear that this method could appear
        // in specToURL(), but the problem is that specToURL() creates
        // a new URL with the spec, so it only does further checks if
        // the URL is malformed.  Unfortunately, in Web Start applications
        // the URL will often refer to a resource in another jar file,
        // which means that the jar url is not malformed, but there is
        // no resource by that name.  Probably specToURL() should return
        // the resource after calling new URL().
        int jarEntry = jarURLString.indexOf("!/");

        if (jarEntry == -1) {
            jarEntry = jarURLString.indexOf("!\\");

            if (jarEntry == -1) {
                return null;
            }
        }

        try {
            // !/ means that this could be in a jar file.
            String entry = jarURLString.substring(jarEntry + 2);

            // We might be in the Swing Event thread, so
            // Thread.currentThread().getContextClassLoader()
            // .getResource(entry) probably will not work.
            Class refClass = Class.forName("ClassUtilities");
            URL entryURL = refClass.getClassLoader().getResource(entry);
            return entryURL;
        } catch (Exception ex) {
            // IOException constructor does not take a cause, so we add it.
            IOException ioException = new IOException("Cannot find \"" + jarURLString + "\".");
            ioException.initCause(ex);
            throw ioException;
        }
    }
}

Related

  1. isUrlReachable(String url)
  2. isValidToc(URL url)
  3. isValidUrl(String input)
  4. isZipName(URI documentIRI, URLConnection connection)
  5. jarURLDirectories(URL jarURL)
  6. lastModifiedURL(String urlstr)
  7. loadCookie(URL url)
  8. loadJar(String path, URL resource)
  9. loadLinesUrl(String url)