In this Java tools tutorial, you will learn how to use the Java compiler via the javac command to compile Java source files (.java) into bytecode files (.class).

Table of content:

    1. Compile a single Java source file
    2. Compile multiple Java source files
    3. Compile a Java source file which has dependencies
    4. Specify destination directory
    5. Specify source path directory
    6. Specify Java source compatibility version
    7. Specify target VM version
    8. Silent compilation and verbosity
You know, the Java Development Kit (JDK) provides javac which is the Java compiler program. It compiles Java source files (.java) into bytecode class files (.class). The tool is located under JDK_HOME\bin directory. So make sure you included this directory in the PATH environment variable so it can be accessed anywhere in command line prompt.

This tutorial summarizes common practices for compiling Java source files from command line. All the commands below are supposing the current working directory is where the source files are placed. Syntax of this command is:

javac [options] [source files]

Type javac -help to view compiler options, and type javac -version to know current version of the compiler. By default, the generated .class files are placed under the same directory as the source files.

 

1. Compile a single Java source file

javac HelloWorld.java 
 

2. Compile multiple Java source files

  • Compile three source files at once, type:
    javac Program1.java Program2.java Program3.java
     

  • Compile all source files whose filenames start with Swing:
    javac Swing*.java
     

  • Compile all source files:
javac *.java 
 

3. Compile a Java source file which has dependencies

It’s very common that a Java program depends on one or more external libraries (jar files). Use the flag -classpath(or -cp) to tell the compiler where to look for external libraries (by default, the compiler is looking in bootstrap classpath and in CLASSPATH environment variable).

  • Compile a source file which depends on an external library:
    javac -classpath mail.jar EmailSender.java

    Or:

    javac -cp mail.jar EmailSender.java
  • Compile a source file which depends on multiple libraries:
    javac -cp lib1.jar;lib2.jar;lib3.jar MyProgram.java


    Or we can use the wildcard character *:

    javac -cp *; MyProgram.java
    That will instruct the compiler to look for all available libraries in the same directory as the source file.

 

4. Specify destination directory

Use the -d directoryoption to specify where the compiler puts the generated .class files. For example:

javac -d classes MyProgram.java
 

NOTES:

      • The compiler will complain if the specified directory does not exist, and it won’t create one.
      • If the source file is under a package, the compiler will create package structure in the destination directory.
 

5. Specify source path directory

We can tell the compiler where to search for the source files by using the -sourcepath directoryoption. For example:

javac -sourcepath src MyProgram.java
 

That will compile the MyProgram.java file in the src directory.

 

6. Specify Java source compatibility version

We can tell the compiler which Java version applied for the source file, by using the -source release option. For example:

javac -source 1.5 MyProgram.java
 

That will tell the compiler using specific language features in Java 1.5 to compile the source file. The valid versions are: 1.3, 1.4, 1.5 (or 5), 1.6 (or 6) and 1.7 (or 7).

 

7. Specify target VM version

The compiler can generate the .class files for a specific Java virtual machine (VM) version. Using the -target release option we can do this, for example: 

javac -target 1.6 -source 1.5 MyProgram.java
 

The target VM version must be greater than or equal the source version, that’s why we specify both the options -target and -source here.

By default, the target VM version is the version of the compiler.

 

8. Silent compilation and verbosity

The compiler can compile source files which are related to the specified one, and it does that silently. So using the -verbose option can tell us what the compiler is doing:

javac -verbose MyProgram.java

Other Java Tools Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#7joe2020-04-25 17:40
I need to change the url address in a jar file. How can I do that. I am not a programmer.
Quote
#6Nam2019-03-18 22:12
Quoting dude with questions:
how do i set up the javac command? it just errors when i put it in windows command prompt

You need to set update the PATH environment variable to include a path to Java home's bin directory. See the steps here:

codejava.net/.../...
Quote
#5dude with questions2019-03-15 12:43
how do i set up the javac command? it just errors when i put it in windows command prompt
Quote
#4SmartManoj2017-08-13 00:26
Quoting Bill:
Using the exact syntax presented in this article, I get the following error message: C:\Work1\Work1B\DataUtilFr (2)>javac -verbose HelloWorld.Java
error: Class names, 'HelloWorld.Java', are only accepted if annotation processin
g is explicitly requested
[total 23ms]
1 error

.Java to .java
caps J to small j
Quote
#3Vicent2017-02-18 06:23
very good article..........................
Quote