The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.net  [27 examples] > URLs  [8 examples]

e138. Getting a Jar File Using a URL

    try {
        // Create a URL that refers to a jar file on the net
        URL url = new URL("jar:http://hostname/my.jar!/");
    
        // Create a URL that refers to a jar file in the file system
        url = new URL("jar:file:/c:/almanac/my.jar!/");
    
        // Get the jar file
        JarURLConnection conn = (JarURLConnection)url.openConnection();
        JarFile jarfile = conn.getJarFile();
    
        // When no entry is specified on the URL, the entry name is null
        String entryName = conn.getEntryName();  // null
    
    
        // Create a URL that refers to an entry in the jar file
        url = new URL("jar:file:/c:/almanac/my.jar!/com/mycompany/MyClass.class");
    
        // Get the jar file
        conn = (JarURLConnection)url.openConnection();
        jarfile = conn.getJarFile();
    
        // Get the entry name; it should be the same as specified on URL
        entryName = conn.getEntryName();
    
        // Get the jar entry
        JarEntry jarEntry = conn.getJarEntry();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

 Related Examples
e132. Creating a URL
e133. Converting Between a URL and a URI
e134. Parsing a URL
e135. Sending a POST Request Using a URL
e136. Getting Text from a URL
e137. Getting an Image from a URL
e139. Accessing a Password-Protected URL

See also: Datagram    Encodings    HTTP    Hostnames and IP Addresses    Multicast    Sockets   


© 2002 Addison-Wesley.