소스 및 클래스 파일 관리하기 Managing Source and Class Files
자바 언어 규격이 요구하고 있지 않지만, 많은 자바 플랫폼의 구현은 소스와 클래스 파일을 관리하기 위한 계층적 파일 시스템에 의존하고 있습니다.
전략은 다음과 같습니다.
이름이 타입의 간단한 이름이고 그 확장자가 .java 인, 텍스트 파일 안의 클래스, 인터페이스, 열거, 또는 주석 타입에 소스 코드를 넣으십시오. 예를 들면:
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, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java
. For 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).
- class name –
graphics.Rectangle
- pathname to file –
graphics\Rectangle.java
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
. Each 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
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
. For 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. However, the path to the .class
files does not have to be the same as the path to the .java
source files. 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. 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.
The full path to the classes
directory, <path_two>\classes
, is called the class path, and is set with the CLASSPATH
system variable. Both the compiler and the JVM construct the path to your .class
files by adding the package name to the class path. 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
<path_two>\classes\com\example\graphics.
A class path may include several paths, separated by a semicolon (Windows) or colon (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.
Setting the CLASSPATH System Variable
To display the current CLASSPATH
variable, use these commands in Windows and UNIX (Bourne shell):
In Windows: C:\> set CLASSPATH In UNIX: % echo $CLASSPATH
To delete the current contents of the CLASSPATH
variable, use these commands:
In Windows: C:\> set CLASSPATH= In UNIX: % unset CLASSPATH; export CLASSPATH
To set the CLASSPATH
variable, use these commands (for example):
In Windows: C:\> set CLASSPATH=C:\users\george\java\classes In UNIX: % CLASSPATH=/home/george/java/classes; export CLASSPATH
'Java 배우기' 카테고리의 다른 글
Questions and Exercises: Creating and Using Packages (0) | 2016.01.24 |
---|---|
Summary of Creating and Using Packages (0) | 2016.01.24 |
Using Package Members (0) | 2016.01.24 |
Naming a Package 패키지 이름짓기 (0) | 2016.01.24 |
Creating a Package 패키지 만들기 (0) | 2016.01.24 |
댓글