Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Java Compiler, javac tool options

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



The Java Compiler Javac Tool Options Use

The javac is the primary Java compiler included in the Java Development Kit (JDK). The javac tool is located in <JAVA_HOME>\jdk1.x\bin folder.

The Java compiler accepts Java language source code and produces byte codes that Java Virtual Machine (JVM) can understand.

The javac tool reads class and interface definitions, written in the Java programming language, and converts them into byte code class files. It can also process annotations in Java source files and classes.

The javac compiler itself is written in Java. The compiler can also be invoked programmatically.


Steps or Phases in Java Source Code Compilation

The Java compiler during compilation process goes through the following phases:

  • The javac tool first checks whether provided java source file is available or not.

  • If the supplied java source file is not available then the Java compiler will give an error javac: file not found: filename.java.

  • If the supplied Java source file is available then the Java compiler loads the Java source code, checks the syntaxes for correctness and makes sure that there are no checked exceptions.

  • Then Java compiler translates the Java source code into the byte code and creates a separate class file( with .class extension) for each class to save the generated byte code.

Note: While generating the byte code from the Java source code the Java compiler does not consider the underlying operating system and processor architecture, that is why Java byte code is platform independent and architecture neutral.


How to compile multiple Java source code files at a time by javac?

You can compile multiple files at a time by three ways, they are:


1. For a small number of source files, simply list the file names on the command line separated by a blank space.

Example:

D:\> javac file1.java file2.java filen.java


2. For large number of Java source code files residing a same package use *.java. This will compile all the java source files of current package.

Example:

D:\> javac *.java


3. For a large number of source files residing in a same package or in different package, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.

Example:

Create a simple text file myfiles.txt

Write the java source files names, each on a separate line like

file1.java

subpackage\file2.java

filen.java

D:\>javac @myfiles.txt


The Java Compiler javac tool options list

There are plenty of standard options available for javac tool, some of them are used and listed below.


1. -cp path or -classpath path

This option specifies where to find user class files, Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path.

Example:

javac -classpath .;D:\users\classes;C:\tools\java\classes ...


2. -d directory

This option allows you to set the destination directory for compiled class files. The directory must already exist; javac will not create it.

Example:

your .java files are in D:\topperskills and you want to store the generated .class files in D:\ topperskills\classes directory. Then execute the command like below.

D:\topperskills> javac –d D: \topperskills\classes *.java

If a Java class in source code added a package at top like package com.topperskills.test; javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d D:\myclasses and the Java class is called com.topperskills.test.MyClass, then the generated class file is called D:\myclasses\com\mypackage\MyClass.class.

If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.


3. -g

Generate all debugging information, including local variables. By default, only line number and source file information is generated.


4. -help

Print a synopsis of standard options.


5. -nowarn

This option is to turn off warnings. If used the compiler does not print out any warnings.


6. verbose

Verbose output. This includes information about each class loaded and each source file compiled.


7. version

Print version information.


Javac Tool Command Line Argument Files

To shorten or simplify the javac command line, you can specify one or more files that themselves contain arguments like options and list of java source files to the javac command (except -J options). This enables you to create javac commands of any length on any operating system.

An argument file can include javac options and source filenames in any combination. The arguments within a file can be space-separated or newline-separated. If a filename contains embedded spaces, put the whole filename in double quotes, and double each backslash ("My Files\\Stuff.java").

Filenames within an argument file are relative to the current directory, not the location of the argument file. Wildcards (*) are not allowed in these lists (such as for specifying *.java). Use of the '@' character to recursively interpret files is not supported. The -J options are not supported because they are passed to the launcher, which does not support argument files.

When executing javac, pass in the path and name of each argument file with the '@' leading character. When javac encounters an argument beginning with the character '@', it expands the contents of that file into the argument list.

Example 1:- Single Arg File

You could use a single argument file named "argfile" to hold all javac arguments:

D:\>javac @argfile

This argument file could contain the contents of both files shown in the next example.


Example 1:- Two Arg Files

You can create two argument files -- one for the javac options and the other for the source filenames: (Notice the following lists have no line-continuation characters.)

Create a file named "options" containing:

-d classes

-g

-sourcepath D:\topperskills\classes

Create a file named "classes" containing:

MyClass1.java

MyClass2.java

MyClass3.java

You can then compile above files by javac like:

D:\>javac @options @classes

Share the article to help your friends