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 :
- getJarFileURL() : Returns the url to jar file for this connection.
Syntax :public URL getJarFileURL()
-
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()
- getJarFile() : Returns the jar file for this connection.
Syntax :public abstract JarFile getJarFile() throws IOException Throws : IOException : If IO exception occurs during connection - 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. -
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 - 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 - getMainAttributes() : Returns the main attributes for this connection.
Syntax :public Attributes getMainAttributes() throws IOException - 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
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
Improved By : ManasChhabra2

