Java Tips & Tricks vol.2

Paths and JARs These are just some tips and tricks I used or read last month that I’d like to share. This list does not contain silver bullets and it doesn’t pretend to be complete or be an absolute truth. It’s just a list that I hope may be helpful for someone. Here is the begging […]

by Boris Velichkov

June 4, 2014

2 min read

tips and tricks - Java Tips & Tricks vol.2

Paths and JARs

These are just some tips and tricks I used or read last month that I’d like to share. This list does not contain silver bullets and it doesn’t pretend to be complete or be an absolute truth. It’s just a list that I hope may be helpful for someone. Here is the begging of the post: Java tips & tricks| Paths and JARs. So, let’s continue:

4. Read a JAR file

As we know JAR files are zip files so to read a jar file is really easy:

[java]
ZipFile file = null;
try {
file = new ZipFile("filename.jar");
} catch (IOException e) {
e.printStackTrace();
}
if (file != null) {
Enumeration<? extends ZipEntry> entries = file.entries();
if (entries != null) {
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// Write your logic here.
}
}
}
[/java]

Also, we can create JAR file programmatically but this won’t be discussed in this post. For more information you can check this link: https://viralpatel.net/blogs/creating-zip-and-jar-files-in-java/

5. Difference between WAR, EAR and JAR files

These files are simply zipped files using java jar tool. These files are created for different purposes. Here is the description of these files:

  • .jar files: These files are with the .jar extension. The .jar files contain the libraries, resources and accessories files like property files.
  • .war files: These files are with the .war extension. The war file contains the web application that can be deployed on the any servlet/jsp container. The .war file contains jsp, html, javascript and other files for necessary for the development of web applications.
  • .ear files: The .ear file contains the EJB modules of the applications.

6.More about JARs

Finally, if you like this post and you are interesting about JAR files you can look at these links:

Share your thoughts with us in the comment section. What tricks do you use?