As you have seen, classes are stored in subdirectories of the file system. The path tothe class must match the package name.
Class files can also be stored in a JAR (Java archive) file. A JAR file contains multipleclass files and subdirectories in a compressed format, saving space and improving performance.When you use a third-party library in your programs, you will usually begiven one or more JAR files to include. The JDK also supplies a number of JAR files,such as the file jre/lib/rt.jar that contains thousands of library classes.
TIP: JAR files use the ZIP format to organize files and subdirectories. You can use any ZIP utility to peek inside rt.jar and other JAR files.
To share classes among programs, you need to do the following:
On UNIX, the elements on the class path are separated by colons:
On Windows, they are separated by semicolons:
In both cases, the period denotes the current directory.
This class path contains
Starting with Java SE 6, you can specify a wildcard for a JAR file directory, like this:or
In UNIX, the * must be escaped to prevent shell expansion.
All JAR files (but not .class files) in the archives directory are included in this class path.The runtime library files (rt.jar and the other JAR files in the jre /lib and jre /lib /extdirectories) are always searched for classes; you don’t include them explicitly in the class path.
CAUTION: The javac compiler always looks for files in the current directory, but the java virtual machine launcher only looks into the current directory if the “.” directory is on the class path. If you have no class path set, this is not a problem—the default class path consists of the “.” directory. But if you have set the class path and forgot to include the “.” directory, your programs will compile without error, but they won’t run.
The class path lists all directories and archive files that are starting points for locating classes. Let’s consider our sample class path:
Suppose the virtual machine searches for the class file of the com .horstmann .corejava. Employee class. It first looks in the system class files that are stored in archives in the jre/lib andjre/lib/ext directories. It won’t find the class file there, so it turns to the class path. It thenlooks for the following files:
The compiler has a harder time locating files than does the virtual machine. If you referto a class without specifying its package, the compiler first needs to find out the package that contains the class. It consults all import directives as possible sources for the class .For example, suppose the source file contains directives
and the source code refers to a class Employee. The compiler then tries to findjava.lang .Employee (because the java .lang package is always imported by default), java .util .Employee, com .horstmann .corejava .Employee, and Employee in the current package. Itsearches for each of these classes in all of the locations of the class path. It is a compile-timeerror if more than one class is found. (Because classes must be unique, the order of theimport statements doesn’t matter.)
The compiler goes one step further. It looks at the source files to see if the source is newerthan the class file. If so, the source file is recompiled automatically. Recall that you canimport only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compilercan easily locate source files for public classes. However, you can import nonpublic classes from the current package. These classes may be defined in source files with differentnames. If you import a class from the current package, the compiler searches allsource files of the current package to see which one defines the class.
Setting the Class Path
It is best to specify the class path with the -classpath (or -cp) option:
The entire command must be typed onto a single line. It is a good idea to place such along command line into a shell script or a batch file.
Using the -classpath option is the preferred approach for setting the class path. An alternate approach is the CLASSPATH environment variable. The details depend on your shell.With the Bourne Again shell (bash), use the command
With the C shell, use the command
With the Windows shell, use
The class path is set until the shell exits.
CAUTION: Some people recommend to set the CLASS PATH environment variable permanently. This is generally a bad idea. People forget the global setting, and then they are surprised when their classes are not loaded properly. A particularly reprehensible example is Apple’s Quick Time installer in Windows. It globally sets CLASSPATH to point to a JAR file that it needs, but it does not include the current directory in the classpath. As a result, countless Java programmers have been driven to distraction when their programs compiled but failed to run.
CAUTION: Some people recommend to bypass the class path altogether, by dropping all JAR files into the jre/lib/ext directory. That is truly bad advice, for two reasons. Archives that manually load other classes do not work correctly when they are placed in the extension directory. (See Volume II, Chapter 9 for more information on class loaders.) Moreover, programmers have a tendency to forget about the files they placed there months ago. Then, they scratch their heads when the class loader seems to ignore their carefully crafted class path, when it is actually loading long-forgotten classes from the extension directory.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.