Documentation

The Java™ Tutorials
Hide TOC
Managing Source and Class Files管理源文件和类文件
Trail: Learning the Java Language
Lesson: Packages
Section: Creating and Using 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. Java平台的许多实现依赖于分层文件系统来管理源文件和类文件,尽管Java语言规范并不要求这样做。The strategy is as follows.策略如下。

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java. 将类、接口、枚举或注释类型的源代码放入一个文本文件中,该文件的名称为该类型的简单名称,扩展名为.javaFor example:例如:

//in the Rectangle.java file 
package graphics;
public class Rectangle {
   ... 
}

Then, put the source file in a directory whose name reflects the name of the package to which the type belongs:然后,将源文件放在一个目录中,该目录的名称反映了该类型所属包的名称:

.....\graphics\Rectangle.java

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (for UNIX, use the forward slash).包成员的限定名和文件的路径名是平行的,假定使用Microsoft Windows文件名分隔符反斜杠(对于UNIX,使用正斜杠)。

As you should recall, by convention a company uses its reversed Internet domain name for its package names. 您应该记得,按照惯例,一家公司使用其反向互联网域名作为其软件包名称。The Example company, whose Internet domain name is example.com, would precede all its package names with com.example. 示例公司的互联网域名为example.com,它的所有软件包名称前面都有com.ExampleEach component of the package name corresponds to a subdirectory. 包名称的每个组件对应一个子目录。So, if the Example company had a com.example.graphics package that contained a Rectangle.java source file, it would be contained in a series of subdirectories like this:因此,如果示例公司有一个com.Example.graphics包,其中包含一个Rectangle.java源文件,它将包含在一系列子目录中,如下所示:

....\com\example\graphics\Rectangle.java

When you compile a source file, the compiler creates a different output file for each type defined in it. 编译源文件时,编译器会为其中定义的每种类型创建不同的输出文件。The base name of the output file is the name of the type, and its extension is .class. 输出文件的基名称是类型的名称,扩展名是.classFor example, if the source file is like this例如,如果源文件如下所示

//in the Rectangle.java file
package com.example.graphics;
public class Rectangle {
      . . . 
}

class Helper{
      . . . 
}

then the compiled files will be located at:然后,编译的文件将位于:

<path to the parent directory of the output files>\com\example\graphics\Rectangle.class
<path to the parent directory of the output files>\com\example\graphics\Helper.class

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. .java源文件一样,编译的.class文件应该位于反映包名的一系列目录中。However, the path to the .class files does not have to be the same as the path to the .java source files. 但是,.class文件的路径不必与.java源文件的路径相同。You can arrange your source and class directories separately, as:您可以分别排列源目录和类目录,如下所示:

<path_one>\sources\com\example\graphics\Rectangle.java

<path_two>\classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. 通过这样做,您可以将classes目录提供给其他程序员,而无需透露您的源代码。You also 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.您还需要以这种方式管理源文件和类文件,以便编译器和Java虚拟机(JVM)能够找到您的程序使用的所有类型。

The full path to the classes directory, <path_two>\classes, is called the class path, and is set with the CLASSPATH system variable. classes目录的完整路径,<path_two>\classes,称为类路径,并使用CLASSPATH系统变量设置。Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. 编译器和JVM都通过将包名添加到类路径来构造.class文件的路径。For example, if例如,如果:

<path_two>\classes

is your class path, and the package name is是类路径,包名称为

com.example.graphics,

then the compiler and JVM look for .class files in然后编译器和JVM在以下位置中查找.class文件

<path_two>\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (UNIX). 类路径可以包括多个路径,用分号(Windows)或冒号(UNIX)分隔。By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.默认情况下,编译器和JVM搜索当前目录和包含Java平台类的JAR文件,以便这些目录自动位于类路径中。

Setting the CLASSPATH System Variable设置CLASSPATH系统变量

To display the current CLASSPATH variable, use these commands in Windows and UNIX (Bourne shell):要显示当前CLASSPATH变量,请在Windows和UNIX(Bourne shell)中使用以下命令:

In Windows:   C:\> set CLASSPATH
In UNIX:      % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use these commands:要删除CLASSPATH变量的当前内容,请使用以下命令:

In Windows:   C:\> set CLASSPATH=
In UNIX:      % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable, use these commands (for example):要设置CLASSPATH变量,请使用以下命令(例如):

In Windows:   C:\> set CLASSPATH=C:\users\george\java\classes
In UNIX:      % CLASSPATH=/home/george/java/classes; export CLASSPATH

Previous page: Using Package Members
Next page: Summary of Creating and Using Packages