270 likes | 437 Views
Chapter 13. Packages. a package , in Java, is a named collection of related classes ; giving meaningful names to a set of related classes in this way makes it easy for programmers to locate these classes when required;
E N D
Packages • a package, in Java, is a named collection of related classes; • giving meaningful names to a set of related classes in this way makes it easy for programmers to locate these classes when required; • the package name actually corresponds to the name of the directory (or folder as some operating systems call it) in which all the given classes reside; • all predefined Java packages themselves reside in a global Java directory, named simply java; • this directory is not itself a package but a store for other packages.
DecimalFormat.class text other class files java ActionListener.class awt event Menu.class other class files other packages Graphics.class Component.class other class files Sample java package hierarchy
Accessing classes in packages To tell the compiler where a class file resides we add an import statement: The asterisk allows you to have access to all class files in the given package.
Import rules • there can only ever be one '.*' in an import statement; • the '.*' must follow a package name; • you can have as many import statements as you require. Examples
Importing specific classes To import a specific class just use the full path name to the class
Importing classes without an 'import' statement To do this, references to any such classes must be appended onto the package name itself:
Avoiding name clashes The package + class name can be useful when the class name on its own clashes with the name of another class. An example • assume we have developed our own class called Graphics; • assume that the constructor for this class takes two integer co-ordinates.
A problem with name clashes We are referencing two Graphics classes here: this will result in a compiler error
Solution Use the extended package name to differentiate between the two classes. Now there is no name clash.
Developing your own packages An example • we will create a unique package for the Hostel example from semester 1; • we will call this package hostelApp. • to do this, add the following package command at the top of each of the original source files: • this line instructs the compiler that the class file created from this source file must be put in a package called hostelApp.
Example: when you compile this class, your Java IDE should create a directory called hostelApp for you and place the Payment class in this directory.:
Importing classes from your own packages If you had developed the hostelApp package in this way, program 12.1 would need to be re-written for it to compile:
Package scope • classes can be made visible outside of their package only if they are declared as public; • unless they are declared as public, classes by default have what is known as package scope. • this means that they are visible only to other classes within the same package. • in the case of the hostel application, we might choose to make only the Hostel class public, and keep all the other files required in this application hidden within the package by giving them package scope.
The classpath • the classpath is a special environmentvariable.; • environment variables provide the operating system with information such as the location of important files in the system; • the special environment variable related to the location of java packages is the classpath variable. • the details of how the classpath environment variable is set will differ from one operating system to another. • in windows, for example, you place an instruction such as the one below in the autoexec.bat file using an application like microsoft notepad. SET CLASSPATH = C:\jCreator\myProjects Note • the classpath is not the location of classes in packages, but the location of packages themselves.
Running applications from the command line • if you are working from a command line you would use the java command (followed by the name of a class) to run an application. • when you run a class that resides in a package you must amend this slightly. • assuming the class RunHostel is not part of a package, it can be run simply from the command line as follows: java RunHostel • if the classpath had not been previously set we could specify it as part of this command.
Running applications from the command line: an example • if the classpath were C:\jCreator\myProjects then we could write java -cp C:\jCreator\myProjects RunHostel Note • the parameter cp, indicating the classpath, is prefixed with a minus sign. • if we had provided a similar class, RunHostelFromPackage, as part of the hostelApp package, we would have to append the class name onto the name of the package: java hostelApp.RunHostelFromPackage
Sending parameters from the command line A main method receives a list of String objects as a parameter They are sent to main from the command line. by listing the strings, one after the other after the name of the class as follows: java ClassName firstString secondString otherStrings
Sending parameters from the command line : An example If a program were called ProcessNames, two names could be sent to it as follows: java ProcessNames Aaron Quentin Were the strings to contain spaces, they must be enclosed in quotes: java ProcessNames "Aaron Kans" "Quentin Charatan"
Running program 13.3 from the command line java ProcessNames "Batman and Robin" Superman This would produce the obvious result: hello Batman and Robin hello Superman
Deploying your packages • a very common way of making your packages available to clients is to convert them to JAR files; • a JAR file (short for Java Archive) has the extension .jar and is simply a compressed file. An example • in the case of the hostelApp package, if we were to call our JAR file hostel.jar, then the structure of the file would have to be: • if, in a Windows environment, the hostel.jar file were placed in the folder C:\jCreator\myProjects, then the correct classpath statement would be: SET CLASSPATH = C:\jCreator\myProjects\hostel.jar
Creating JAR files • the JAR file acts as the directory in which the package resides. • a JAR file such as the one above can by created using any compression software, such as WinZip, and then changing the extension to .jar. • however, the usual way to create such a file is to use the jar.exe application; • this is provided with the SUN software development kit and also with most standard IDEs.
Running the jar.exe program from the command line • assuming that the hostelApp folder that contains the required .class files is in the same directory as the jar.exe application (or that the system path is appropriately set to provide access to it) then the correct statement to create the above package is: jar cvf hostel.jar hostelApp c: create a new JAR file; v: provide full (verbose) output to report on progress; f: provide a name for the JAR file. • after these switches comes the name of the output file - hostel.jar; • finally we must list the files we wish to be included; • in the above example we require a properly structured file, so we just have to include the name of the directory where the files reside, hostelApp.
Creating "executable" JAR files • if you are working in a graphics environment, and there is a JVM installed on your computer, then it is possible to create a JAR file that will run the program by double-clicking on its icon. • in this case, we must include a file called a manifest file (which normally has the extension .mf). • if we want the JAR file to be executable the manifest file must contain information about which class in the package is the one with the main method. • if we assume that the main class is called RunHostel, our manifest file would look like this: Manifest-Version: 1.0 Main-Class: RunHostel now we could create the executable file with the following line: jar cvfm RunHostel.jar manifest.mf RunHostel.class hostelApp • the additional "m" switch after the "c", "v" and "f" switches indicates that we wish to include the manifest file stated. • now double-clicking on the resulting JAR file's icon will run the application.
Creating "executable" files in Windows • if you are using a Windows operating system then instead of creating a .JAR file, you can create a shortcut to the file javaw.exe; • you can then set the shortcut's properties so that the main class is added as a parameter.