站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Managing Source and Class Files - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Interfaces and Packages

Managing Source and Class Files

Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this. The strategy is as follows.

Put the source code for a class, interface, enum, or annotation in a text file whose name is the simple name of the type and whose extension is .java. Then, put the source file in a directory whose name reflects the name of the package to which the type belongs. For example, the source code for the Rectangle class would be in a file named Rectangle.java, and the file would be in a directory named graphics. The graphics directory might be anywhere on the file system. The following figure shows how this works.

The source code for the Rectangle class is in the file Rectangle.java which is located in a folder named graphics.

The source code for the Rectangle class is in the file Rectangle.java which is located in a folder named graphics.

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (\).

class name graphics.Rectangle
pathname to file graphics\Rectangle.java

As you may recall, by convention a company uses its reversed Internet domain name for its package names. The sample company whose Internet domain name is hobnob.com would precede all its package names with com.hobnob. Each component of the package name corresponds to a subdirectory. So, if Hobnob had a graphics package that contained a Rectangle.java source file, it would be contained in a series of subdirectories, as shown in the following figure.

By convention, companies use their Internet domain names in reverse for their package names.

By convention, companies use their Internet domain names in reverse for their package names.

When you compile a source file, the compiler creates a different output file for each class and interface defined in it. The base name of the output file is the name of the class or the interface, and its extension is .class, as shown in the following figure.

The compiler creates a separate .class file for every class.

The compiler creates a separate .class file for every class.

Like a .java file, a .class file should also be in a series of directories that reflect the package name; however, it does not have to be in the same directory as its source. You could arrange your source and class directories separately, as shown below.

An example of how to arrange your source code and class files separately.

An example of how to arrange your source code and class files separately.

By doing this, you can give the classes directory to other programmers without revealing your sources. Then, you can use the -d option for the Java compiler to specify where the class files should go by using the following.

javac -d classes  sources\com\hobnob\graphics\Rectangle.java

Why all the bother about directories and file names? Because you need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses. When the compiler encounters a new class as it's compiling your program, it must be able to find the class in order to resolve names, do type checking, and so on. Similarly, when the JVM encounters a new class as it's running your program, it must be able to find the class to invoke its methods, and so on. Both the compiler and the JVM search for classes in each directory or JAR file listed in your class path.


Definition: A class path is an ordered list of directories or JAR files in which to search for class files.
Each directory listed in the class path is a top-level directory in which package directories appear. From the top-level directory, the compiler and the JVM can construct the rest of the path based on the package and the class name for the class. For example, the class path entry for the directory structure shown in the previous figure would include classes but not com or any of the directories below com. Both the compiler and the JVM construct the path name to a .class file with its full package name.

By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform class files. In other words, the current directory and the Java platform class files are automatically in your class path. Most, if not all, classes can be found in these two locations. So, it's likely that you don't have to worry about the class path. In some cases, however, you might have to set your class path.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.