Compiling A Java Source
Home ] Up ] [ Compiling A Java Source ] Running A Java Application ] Running A Java Applet ]

 

 

To compile a Java source file, you type the following to invoke the bare SDK Java compiler:

javac HelloWorld.java 

(the .java filetype is required -- HelloWorld.java refers to the filename)

If successful, this produces a HelloWorld.class file from the HelloWorld.java file.

The general syntax for invoking the javac compiler is:

javac [ options ] [ sourcefiles ] [ @files ]

where:

Command line parameter Meaning
options Command-line options.
sourcefiles One or more source files to be compiled (such as MyClass.java).
@files One or more files that list source files.

Options include (this is a subset of the full set of options;  see the Java 2 SDK Tools and Utilities documentation for details):

Option Explanation
-classpath path Sets the user class path, overriding the user class path in the CLASSPATH environment variable. 
If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory.
-d directory Sets the destination directory for class files. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d c:\myclasses and the class is called com.mypackage.MyClass, then the class file is called c:\myclasses\com\mypackage\MyClass.class
If -d is not specified, javac puts the class file in the same directory as the source file.
-deprecation Shows a description of each use or override of a deprecated member or class. Without -deprecation, javac shows the names of source files that use or override deprecated members or classes.
-g Generates all debugging information, including local variables. By default, only line number and source file information is generated.
-sourcepath sourcepath Specifies the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.  
Note that classes found through the classpath are subject to automatic recompilation if their sources are found.
-verbose Verbose output. This includes information about each class loaded and each source file compiled.
 

This page was last modified on 02 October, 2007