Skip to content

JAR Files and Namespaces (Packages)

Estimated time to read: 2 minutes

Overview

History

Originally, Java kept its class files in the directory system, overtime however, structures can become very complicated with lots of small files.

As Java was originally designed for the web and for browsers, this means that as one applet loaded, it may then call for another file, and another. This introduced extreme overhead when working with complex applications as many hundreds or thousands of requests had to be made.

To simplify the deployment process, Sun introduced a program that collects all of the class files into one file called JAR. They are pretty much just glorified Zip files.

Present JAR files

Code inside of a JAR file can be executed as is. The files to do not need to be extracted to be usable. Items within the JAR file are loaded by the JVM directly into RAM.

JAR files are used to deploy the libraries used by the development and runtime environments. SpringBoot for example, is a JAR approx 50MB in size, however, it contains all of the classes that make up SpringBoot.

JARs and Namespaces

While working with a JAR file, it is possible that some of the classes in it have the same names in different packages. This is called a namespace collision.

The packaging system helps to deal with namespace collisions:

  • Packages allow you to qualify the simple name of a class.
  • Classes with the same simple name and different package names represent different classes.
  • com.lq.test is not the same class as io.entityfour.test

Package Review

  • Packages provide a platform-neutral directory system for Java
  • Packages allow you to have two different classes with the same name, as long as they are in different packages
  • Packages are very useful:
  • To make classes easier to find and use
  • To organise the project program elements
  • To avoid naming conflicts