Java.net.JarURLConnection class in Java

Prerequisite – JAR files in Java
What is a Jar file?
JavaArchive(JAR) bundles all the classes in one package. Since the archive is compressed and can be downloaded in a single HTTP connection, it is often faster to download the archive than to download individual classes. Although jar bundles all the classes, their fully classified names can be used to refer to individual classes in case of need. Java provides several mechanisms to extract information from a jar file. This article presents one of the ways using JarURLConnection class.
This class represents a URL connection to a jar file, entry or directory. It can be used in scenarios when the programmer knows that the URL refers to a jar entry and needs jar specific functionality.
Syntax of a Jar URL :-
Jar URL starts with the general URL that points to the location of jar archive. Than “jar:” protocol is prefixed and finally “!/” and path to the file inside the jar archive is suffixed in the end of this URL. For example,

jar:http://www.abcd.com/networking.jar!/com/foo/example.class

If the path part is not used, then the URL points to the entire jar archive. For example,

jar:http://www.abcd.com/networking.jar!/

Constructor : Creates a jar URL connection to the specified URL.



Syntax :protected JarURLConnection(URL url)
                    throws MalformedURLException
Parameters :
url : URL to a jar archive
Throws : 
MalformedURLException : If no protocol identified 
or string could not be parsed.

Methods :

  1. getJarFileURL() : Returns the url to jar file for this connection.
    Syntax :public URL getJarFileURL()
  2. getEntryName() : Returns the entry name for this connection, or null if this points to a jar file rather than jar entry file.

    Syntax :public String getEntryName()
  3. getJarFile() : Returns the jar file for this connection.
    Syntax :public abstract JarFile getJarFile()
                                throws IOException
    Throws : 
    IOException : If IO exception occurs during connection
  4. getManifest() : Returns the manifest for this connection, or null if none exists. The manifest is a special file that can contain information about the files packaged in a JAR file.
    Syntax :public Manifest getManifest()
                         throws IOException
    Throws :
    IOException : If IO exception is thrown while connecting to the url.
  5. getJarEntry() : Returns the JAR entry object for this connection. Entry point for a java aplication is usually the class having the method public static void main(String args[]). This method returns null if the url points to a jar file rather than an entry.

    Syntax : public JarEntry getJarEntry()
                         throws IOException
  6. getAttributes() : Returns attributes for this connection if URL points to jar entry file. For more information about the attributes, please visit Official Java Tutorials.
    Syntax :public Attributes getAttributes()
                             throws IOException
  7. getMainAttributes() : Returns the main attributes for this connection.
    Syntax :public Attributes getMainAttributes()
                                 throws IOException
  8. getCertificates() : Returns the certificate object for this connection if it points to jar entry file.
    Syntax : public Certificate[] getCertificates()
                                  throws IOException

The following program assumes that we have a jar file at the URL hard-coded in the program. As can be seen, the hard-coded URL points to a file on the system only, this class can be used when using jar files over the network by specifying the correct URL for the file.
Java Implementation :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate various
// jarURLConnection class methods 
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
  
public class jarurltest 
{
  
    private final static String JAR_URL = "jar:file:/C:/Users/Rishabh/Desktop"
                                     "/testClass.jar!/test/testclass.class";
  
    public static void main(String[] args) throws Exception 
    {
  
        try {
  
            // Create a URL that refers to a jar file in the file system
            URL FileSysUrl = new URL(JAR_URL);
  
            // Create a jar URL connection object
            JarURLConnection jarURLConn = (JarURLConnection) FileSysUrl.openConnection();
  
            // getJarFileURL() method
            System.out.println("Jar file URL : " + jarURLConn.getJarFileURL());
  
            // getEntryName() method
            System.out.println("Entry Name : " + jarURLConn.getEntryName());
  
            // getJarFile() method
            JarFile jarFile = jarURLConn.getJarFile();
            System.out.println("Jar Entry: " + jarURLConn.getJarEntry());
  
            // getManifest() method
            Manifest manifest = jarFile.getManifest();
            System.out.println("Manifest :" + manifest.toString());
  
            // getJarEntry() method
            JarEntry jentry = jarURLConn.getJarEntry();
            System.out.println("Jar Entry : " + jentry.toString());
  
            // getAttributes() method
            Attributes attr = jarURLConn.getAttributes();
            System.out.println("Attributes : " + attr);
  
            // getMainAttributes() method
            Attributes attrmain = jarURLConn.getMainAttributes();
            System.out.println("\nMain Attributes details: ");
  
            for (Entry e : attrmain.entrySet()) 
            {
                System.out.println(e.getKey() + " " + e.getValue());
            }
  
            // getCertificates() method
            Certificate cert[] = jarURLConn.getCertificates();
            System.out.println("\nCertificates Info :" + Arrays.toString(cert));
  
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  
    }
  
}

chevron_right


Output :

Jar file URL : file:/C:/Users/Rishabh/Desktop/testClass.jar
Entry Name : test/testclass.class
Jar Entry: test/testclass.class
Manifest :java.util.jar.Manifest@2f1a6037
Jar Entry : test/testclass.class
Attributes : null

Main Attributes details: 
Manifest-Version 1.0
Main-Class test.testclass

Certificates Info :null

Related Article: Working with JAR and Manifest files In Java

References :Official Java Documentation
Book : Java Network Programming, By Elliotte Rusty Harold

This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : ManasChhabra2



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.