Java: Programming



Managing Source and Class Files


Many implementations of the Java platform rely on hierarchical file systems to manage source and class files.
Put the source code for a class Rectangle in a directory whose name reflects the name of the package to which the type belongs:

..../graphics/Rectangle.java
Rectangle.java:

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

class Helper{
      . . . 
}

The qualified name of the package member and the path name to the file are parallel,
  • class name
  • graphics.Rectangle
  • pathname to file
  • graphics/Rectangle.java

By convention a company uses its reversed Internet domain name for its package names, each component of the package name corresponds to a subdirectory.
The Example company, whose Internet domain name is example.com, would precede all its package names with com.example.
If the Example company had a com.example.graphics package that contained a Rectangle.java source file, it would be contained in a subdirectories like this:

.../com/example/graphics/Rectangle.java

When you compile a source file, the compiler creates a different output file for each class defined in it. The base name of the output file is the name of the class name, and its extension is .class. For the above example, he compiled files will be located at:

.../com/example/graphics/Rectangle.class
.../com/example/graphics/Helper.class


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.
The full path to the classes directory 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.
That is, the compiler and JVM look for .class files defined in CLASSPATH.
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.

To set the CLASSPATH variable, use these commands (for example):
  • In Windows
  • 
       set CLASSPATH=C:\users\george\java\classes
    
  • In UNIX
  • 
       CLASSPATH=/home/george/java/classes; export CLASSPATH
    





留言

熱門文章