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,
If the path part is not used, then the URL points to the entire jar archive. For example,
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.
- 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 :
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 firstname.lastname@example.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 Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.