Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Compiling Java *.class Files with javac

$
0
0

1. Overview

This tutorial will introduce the javac tool and describes how to use it to compile Java source files into class files.

We’ll get started with a short description of the javac command, then examine the tool in more depth by looking at its various options.

2. The javac Command

We can specify options and source files when executing the javac tool:

javac [options] [source-files]

Where [options] denotes the options controlling operations of the tool, and [source-files] indicates one or more source files to be compiled.

All options are indeed entirely optional. Source files can be directly specified as arguments to the javac command or kept in a referenced argument file as described later. Notice that source files should be arranged in a directory hierarchy corresponding to the fully qualified names of the types they contain.

Options of javac are categorized into three groups: standard, cross-compilation, and extra. In this article, we’ll focus on the standard and extra options.

The cross-compilation options are used for the less common use case of compiling type definitions against a JVM implementation different from the compiler’s environment and won’t be addressed.

3. Type Definition

Let’s start by introducing the class we’re going to use to demonstrate the javac options:

public class Data {
    List<String> textList = new ArrayList();

    public void addText(String text) {
        textList.add(text);
    }

    public List getTextList() {
        return this.textList;
    }
}

The source code is placed in the file com/baeldung/javac/Data.java.

Note that we use *nix file separators in this article; on Windows machines, we must use the backslash (‘\’) instead of the forward slash (‘/’).

4. Standard Options

One of the most commonly used standard options of the javac command is -d, specifying the destination directory for generated class files. If a type isn’t part of the default package, a directory structure reflecting the package’s name is created to keep the class file of that type.

Let’s execute the following command in the directory containing the structure provided in the previous section:

javac -d javac-target com/baeldung/javac/Data.java

The javac compiler will generate the class file javac-target/com/baeldung/javac/Data.class. Note that on some systems, javac doesn’t automatically create the target directory, which is javac-target in this case. Therefore, we may need to do so manually.

Here are a couple of other frequently used options:

  • -cp (or -classpath, –class-path) – specifies where types required to compile our source files can be found. If this option is missing and the CLASSPATH environment variable isn’t set, the current working directory is used instead (as was the case in the example above).
  • -p (or –module-path) –  indicates the location of necessary application modules. This option is only applicable to Java 9 and above – please refer to this tutorial for a guide to the Java 9 module system.

If we want to know what’s going on during a compilation process, e.g. which classes are loaded and which are compiled, we can apply the -verbose option.

The last standard option we’ll cover is the argument file. Instead of passing arguments directly to the javac tool, we can store them in argument files. The names of those files, prefixed with the ‘@ character, are then used as command arguments.

When the javac command encounters an argument starting with ‘@, it interprets the following characters as the path to a file and expands the file’s content into an argument list. Spaces and newline characters can be used to separate arguments included in such an argument file.

Let’s assume we have two files, named options, and types, in the javac-args directory with the following content:

The options file:

-d javac-target
-verbose

The types file:

com/baeldung/javac/Data.java

We can compile the Data type like before with detail messages printed on the console by executing this command:

javac @javac-args/options @javac-args/types

Rather than keeping arguments in separate files, we can also store them all in a single file.

Suppose there is a file named arguments in the javac-args directory:

-d javac-target -verbose
com/baeldung/javac/Data.java

Let’s feed this file to javac to achieve the same result as with the two separate files before:

javac @javac-args/arguments

Notice the options we’ve gone through in this section are the most common ones only. For a complete list of standard javac options, check out this reference.

5. Extra Options

Extra options of javac are non-standard options, which are specific to the current compiler implementation and may be changed in the future. As such, we won’t go over these options in detail.

However, there is an option that’s very useful and worth mentioning, -Xlint. For a full description of the other javac extra options, follow this link.

The -Xlint option allows us to enable warnings during compilation. There are two ways to specify this option on the command line:

  • -Xlint – triggers all recommended warnings
  • -Xlint:key[,key]* – enables specific warnings

Here are some of the handiest -Xlint keys:

  • rawtypes – warns about the use of raw types
  • unchecked –  warns about unchecked operations
  • static – warns about the access to a static member from an instance member
  • cast – warns about unnecessary casts
  • serial – warns about serializable classes not having a serialversionUID
  • fallthrough – warns about the falling through in a switch statement

Now, create a file named xlint-ops in the javac-args directory with the following content:

-d javac-target
-Xlint:rawtypes,unchecked
com/baeldung/javac/Data.java

When running this command:

javac @javac-args/xlint-ops

we should see the rawtypes and unchecked warnings:

com/baeldung/javac/Data.java:7: warning: [rawtypes] found raw type: ArrayList
    List<String> textList = new ArrayList();
                                ^
  missing type arguments for generic class ArrayList<E>
  where E is a type-variable:
    E extends Object declared in class ArrayList
com/baeldung/javac/Data.java:7: warning: [unchecked] unchecked conversion
    List<String> textList = new ArrayList();
                            ^
  required: List<String>
  found:    ArrayList
...

6. Conclusion

This tutorial walked through the javac tool, showing how to use options to manage the typical compilation process.

In reality, we usually compile a program using an IDE or a build tool rather than directly relying on javac. However, a solid understanding of this tool will allow us to customize the compilation in advanced use cases.

As always, the source code for this tutorial can be found over on GitHub.


Viewing all articles
Browse latest Browse all 4535

Trending Articles