Packages in Java

It is necessary in software development to create several classes and interfaces. After creating these classes and interfaces, it is better if they are divided into some groups depending on their relationship. Thus, the classes and interfaces which handle similar or same task are put into the same directory. This directory or folder is also called as 'package'.

Advantages

  • Packages are useful to arrange related classes and interfaces into a group. This puts together all the classes and interfaces performing the same task in the same package.

  • Packages hide the classes and interfaces in a separate sub directory, so that accidental deletion of classes and interfaces will not take place.

  • The classes and interfaces of a package are isolated from the classes and interfaces of another package. This means we can use same names for classes of two different packages.

  • A group of packages is called a library. The classes and interfaces of a package are like books in a library and can be reused several times.

Types Of Packages

There are two types of packages in java. They are as follows

Built-in packages

These are in-built packages already present in the java programming language. Whenever we need these packages we can import these packages into our program and use them.

User-defined packages

Just like built-in packages, the users of the java language can also create their own packages. They are called user-defined packages. User-defined packages can also be imported into other classes and used exactly in the same way as the built-in packages.

Built-In Packages

These packages which are already available in java language. These packages provide nearly all the necessary classes, interfaces and methods for the programmer to perform any task in his or her programs. Some of the important packages are as follows

  • java.lang - here, lang stands for language, this package got primary classes and interfaces essential for developing a basic java program. It also contains wrapper classes which are useful to convert primitive data types into objects.

  • java.util - here, util means utility, this package contains useful classes and interfaces like stack, arrays, linkedlist, vector, etc. These classes are called as collections.

  • java.io - here, io stands for input and output, this package contains streams. A stream represent flow of data from one place to another place.

  • java.awt - here, awt stands for abstract window toolkit, this package helps to develop GUI (Graphical user interface) where programs with colorful screens, paintings and images etc., can be developed.

  • java.swing - this package helps to develop GUI like java.awt. The x in javax represents that it is an extended package which means it is a package developed from another package by adding new features to it.

  • java.net - here, net stands for network, client-server programming can be done by using this package.

  • java.applet - applets are programs which come from a server into a client and get executed on the client machine on a network.

  • java.text - this package has two important classes i.e. DateFormat and NumberFormat which are useful to format numeric values

  • java.sql - here, sql stands for structured query language, this package helps to connect to databases like Oracle or Sybase, retrieve the data from them and use it in a java program.

Jar Files

JAR stands for java archive file which contains compressed version of .class files, audio files, image files or directories. We can imagine a .jar file as a (.Zip) that is created by using WinZip software. Even, WinZip software can be used to extract the contents of a .jar file. The difference is that a .jar file can be used as it is but whereas the .zip file can not be used directly.

  • To create .jar file we use following command
jar cf jarfilename inputfiles

Here, cf represents create file.

  • To view contents of a .jar file, we can use the following jar command
jar tf jarfilename 

Here, tf represents table view of file contents.

  • To extract the files from a .jar file, we use the following command
jar xf jarfilename 

Here, xf represents the extract files from the jar file.

Subscribe For More Content