Home

How to Use the javac Command

|
|  Updated:  
2016-03-26 16:03:22
|   From The Book:  
No items found.
Java Essentials For Dummies
Explore Book
Buy On Amazon

The javac command in Java compiles a program from a command prompt. It reads a Java source program from a text file and creates a compiled Java class file. The basic form of the javac command is

javac filename <i>[options]</i>

For example, to compile a program named HelloWorld.java, use this command:

javac HelloWorld.java

Normally, the javac command compiles only the file that you specify on the command line, but you can coax javac into compiling more than one file at a time by using any of the following techniques:

  • If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java file in the same folder, the Java compiler automatically compiles that class, too.

  • You can list more than one filename in the javac command. The following command compiles three files:

    javac TestProgram1.java TestProgram2.java TestProgram3.java
  • You can use a wildcard to compile all the files in a folder, like this:

    javac *.java
  • If you need to compile a lot of files at the same time but don’t want to use a wildcard (perhaps you want to compile a large number of files but not all the files in a folder), you can create an argument file, which lists the files to compile. In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. Here’s an argument file named TestPrograms that lists three files to compile:

    TestProgram1.java
    TestProgram2.java
    TestProgram3.java

    You can compile all the programs in this file by using an @ character, followed by the name of the argument file on the javac command line, like this:

javac @TestPrograms

The javac command has a gaggle of options that you can use to influence how it compiles your programs.

Option Description
-bootclasspath Overrides locations of bootstrap class files. (The bootstrap class files are the classes that implement the Java runtime. You will rarely use this option.)
-classpath Specifies where to find user class files. Use this option if your program makes use of class files that you’ve stored in a separate folder.
-cp Same as classpath.
-d Specifies where to place generated class files.
-deprecation Outputs source locations where deprecated APIs (features that are considered obsolete) are used. Use this option if you want the compiler to warn you whenever you use API methods that have been deprecated.
-encoding Specifies character encoding used by source files.
-endorseddirs Overrides location of endorsed standards path.
-extdirs Overrides locations of installed extensions.
-g Generates all debugging info.
-g:{lines,vars,source} Generates only some debugging info.
-g:none Generates no debugging info.
-help Prints a synopsis of standard options.
-J Passes directly to the runtime system.
-nowarn Generates no warnings.
-source Provides source compatibility with specified release.
-sourcepath Specifies where to find input source files.
-target Generates class files for specific virtual machine version.
-verbose Outputs messages about what the compiler is doing.
-version Provides version information.
M Prints a synopsis of nonstandard options.

A class file is a compiled Java program that can be executed by the java command. The Java compiler reads source files and creates class files.

To use one or more of these options, type the option before or after the source filename. Either of the following commands, for example, compiles the HelloApp.java file with the -verbose and -deprecation options enabled:

javac HelloWorld.java –verbose –deprecation
javac –verbose –deprecation HelloWorld.java

About This Article

This article is from the book: 

No items found.

About the book author:

Doug Lowe is the information technology director at Blair, Church & Flynn Consulting Engineers, a civil engineering firm. He has written more than 50 For Dummies books on topics ranging from Java to electronics to PowerPoint.