19.Packages

Friday, April 9, 2010 Posted by Sudarsan
Packages

Packages are Java’s means of grouping related classes and interfaces together in a single
unit (interfaces will be discussed later). This powerful feature provides for a convenient
mechanism for managing a large group of classes and interfaces while avoiding potential
naming conflicts.

Importing Packages
To be able to use classes outside of the package you are currently working in, you need
to import the package of those classes. By default, all your Java programs import the
java.lang.* package, that is why you can use classes like String and Integers inside the program eventhough you haven't imported any packages.

The syntax for importing packages is as follows,

import ;

For example, if you want to use the class Color inside package awt, you have to type the following,

import java.awt.Color;
import java.awt.*;

The first statement imports the specific class Color while the other imports all of the
classes in the java.awt package.

Another way to import classes from other packages is through explicit package
referencing. This is done by using the package name to declare an object of a class.
java.awt.Color color;

Creating your own packages

To create our own package, we write,

package ;

Suppose we want to create a package where we will place our StudentRecord class, together with other related classes. We will call our package, schoolClasses.

The first thing you have to do is create a folder named schoolClasses. Copy all the
classes that you want to belong to this package inside this folder. After copying, add the
following code at the top of the class file. For example,


package schoolClasses;
public class StudentRecord
{
private String name;
private String address;
private int age;
:

Packages can also be nested. In this case, the Java interpreter expects the directory
structure containing the executable classes to match the package hierarchy.

Setting the CLASSPATH

Now, suppose we place the package schoolClasses under the C:\ directory. We need to
set the classpath to point to that directory so that when we try to run it, the JVM will be
able to see where our classes are stored.

Before we discuss how to set the classpath, let us take a look at an example on what will
happen if we don't set the classpath.

Suppose we compile and then run the StudentRecord class we wrote in the last section,

C:\schoolClasses>javac StudentRecord.java
C:\schoolClasses>java StudentRecord
Exception in thread "main" java.lang.NoClassDefFoundError:
StudentRecord (wrong name: schoolClasses/StudentRecord)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(UnknownSource)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(NativeMethod)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(UnknownSource)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(UnknownSource)

We encounter a NoClassDefFoundError which means that Java did not know where tolook for your class. The reason for this is that your class StudentRecord now belongs to a package named studentClasses. If we want to run our class, we need to tell Java about its full class name which is schoolClasses.StudentRecord. We also have to tell JVM where to look for our packages, which in this case is in location C:\. To do this, we must set the classpath.

To set the classpath in Windows, we type this at the command prompt,

C:\schoolClasses> set classpath=C:\
where C:\ is the directory in which we have placed the packages. After setting the
classpath, we can now run our program anywhere by typing,
C:\schoolClasses> java schoolClasses.StudentRecord

Take note that you can set the classpath anywhere. You can also set more than one
classpath, we just have to separate them by ;(for windows)

For example,
set classpath=C:\myClasses;D:\;E:\MyPrograms\Java
Labels:

Post a Comment