Open In App

Java.net.JarURLConnection class in Java

Last Updated : 25 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 to use 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 the 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 application 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 :




// 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();
        }
  
    }
  
}


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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads