Importing Packages Creating your own packages Setting the CLASSPATH

J.E.D.I

10.8 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.

10.8.1 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 havent imported any packages. The syntax for importing packages is as follows, import nameOfPackage; 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;

10.8.2 Creating your own packages

To create our own package, we write, package packageName; 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. Introduction to Programming I 164 J.E.D.I

10.8.3 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 dont set the classpath. Suppose we compile and then run the StudentRecord class we wrote in the last section, C:\schoolClassesjavac StudentRecord.java C:\schoolClassesjava StudentRecord Exception in thread main java.lang.NoClassDefFoundError: StudentRecord wrong name: schoolClassesStudentRecord at java.lang.ClassLoader.defineClass1Native Method at java.lang.ClassLoader.defineClassUnknown Source at java.security.SecureClassLoader.defineClassUnknown Source at java.net.URLClassLoader.defineClassUnknown Source at java.net.URLClassLoader.access100Unknown Source at java.net.URLClassLoader1.runUnknown Source at java.security.AccessController.doPrivilegedNative Method at java.net.URLClassLoader.findClassUnknown Source at java.lang.ClassLoader.loadClassUnknown Source at sun.misc.LauncherAppClassLoader.loadClassUnknown Source at java.lang.ClassLoader.loadClassUnknown Source at java.lang.ClassLoader.loadClassInternalUnknown Source We encounter a NoClassDefFoundError which means that Java did not know where to look 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 jave 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 For Unix base systems, suppose we have our classes in the directory usrlocalmyClasses, we write, export classpath=usrlocalmyClasses Introduction to Programming I 165 J.E.D.I 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 and : for Unix based systems. For example, set classpath=C:\myClasses;D:\;E:\MyPrograms\Java and for Unix based systems, export classpath=usrlocaljava:usrmyClasses Introduction to Programming I 166 J.E.D.I

10.9 Access Modifiers