Java Reference
In-Depth Information
public class MyProgram {
public static void main(String[] args) {
System.out.println("Rome wasn't burned in a day!");
}
}
MyProgram.java
This just outputs a line of text to the command line when it executes. As this is just to try out the compiler,
I'm not explaining how the program works at this point. Of course, you must type the code in exactly as
shown and save it in a file with the name MyProgram.java . The file name without the extension is always
the same as the class name in the code. If you have made any mistakes the compiler issues error messages.
If you need to override an existing definition of the CLASSPATH environment variable — perhaps because
it has been set by a Java development system you have installed — the command would be the following:
javac -classpath . MyProgram.java
The value of CLASSPATH follows the -classpath option specification and here it is just a period. A period
defines the path to the current directory, whatever that happens to be. This means that the compiler looks for
your source file or files in the current directory. If you forget to include the period, the compiler is not able to
find your source files in the current directory. If you include the -classpath . command-line option in any
event it does no harm. If you need to add more paths to the CLASSPATH specification, they must be separated
by semicolons. If a path contains spaces, it must be delimited by double quotes.
Note that you should avoid storing your source files within the directory structure that was created for the
JDK, as this can cause problems. Set up separate directories of your own to hold the source code for your
programs and keep the code for each program in its own directory.
Assuming your program contains no errors, the compiler generates a bytecode program that is the equi-
valent of your source code. The compiler stores the bytecode program in a file with the same name as the
source file, but with the extension .class . Java executable modules are always stored in a file with the ex-
tension .class . By default, the .class file is stored in the same directory as the source file.
The command-line options I have introduced here are by no means all the options you have available for
the compiler. You are able to compile all of the examples in the topic just knowing about the options I have
discussed. There is a comprehensive description of all the options within the documentation for the JDK.
You can also specify the -help command-line option to get a summary of the standard options you can use.
To get a summary of the Java compiler options, enter the following on the command line:
javac -help
To get usage information for the java application launcher, enter the following command:
java -help
If you are using some other product to develop your Java programs, you are probably using a much more
user-friendly, graphical interface for compiling your programs that doesn't involve entering commands such
Search WWH ::




Custom Search