DEV Community

Adam Crockett 🌀
Adam Crockett 🌀

Posted on

What is a jar?

So after many years of using java -jar, It has finally clicked that jar is also a command in terminal.

If we run man jar we see that jar files and the jar utility is a form of archive like .zip and so is .war, hu yeah.

Why does jar exist? Im not sure, Java is a strange ecosystem

Top comments (4)

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

jar stands for Java archive. It exists so that you know that it's a zip that contains Java classes (with or without a main method).

war stands for web archive. It exists so that you know that it's a jar with an additional manifest for Java application servers like Tomcat.

There's also ear for enterprise application archive (J2EE) and IBM invented bar (broker archive) for its message broker platform. Again, both are zip files, but with additional meta files.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Excellent description 😊, so no main means no entry point?

Also I think I see the point but why not just have one file format with a manifest to describe the cases instead of file formats.

Lastly why distribution in an archive?

All very interesting, I should learn java

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Exactly. JARs can be libraries (no entry point) as well as applications, in which case there hast to be a main method as entry point... and I think application JARs also need a manifest file, which mentions the entry point explicitly (e.g. in case there are more than one main methods).

Why not just one file format? Actually it is one file format. The format is: a zip container. The file extension actually is not that important... it's more like a hint, but it does not define the file format or type. The first bytes of a file define its type, see Wikipedia.

And lastly: well a single file is easier to handle than hundreds of files, so they've put it in a container. Maybe you ask: why a ZIP? I think it's a good choice: it's widespread (available on all operating systems), it's free to use (no patents), and it has nice features like optional compression. Optional? Yes, you can use a ZIP container without compressing the files. And I don't think that compiled java class files can be compressed very much, so compression is definitely not the main reason for why they've chosen ZIP.

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

This is an excellent analysis thank you I learned alot today 😁