Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Understanding Javac and Its Purpose
Using javac to Compile Java Code
Commonly Used Options
Frequently Asked Questions
What is javac, and what does it do?
How do I use the Javac command to compile Java code?
What are some commonly used options with Javac?
How can I handle multiple source files with Javac?
What are the best practices for using Javac effectively?
Last Updated: Mar 27, 2024

How to Use the Javac Command

Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


Java, one of the most popular and widely used programming languages in the world, has a feature that you just need to write the code once. Then you can run it anywhere, enabling Java programs to execute on any platform with a Java Virtual Machine (JVM). To run the byte code of Java, we need Javac. This crucial step is accomplished using the Javac command, part of the Java Development Kit (JDK). 


In this article, we will understand how to use the Javac Command and also the example and commonly used options.

Read more, how to run java program

Understanding Javac and Its Purpose

Javac is the command-line Java compiler included in the JDK. It basically converts the code written by us into machine code. And the machine code ends with the extension .java into bytecode files. Which is generally suffixed with .class.
This platform is an independent representation of the original source code, allowing it to run on any system with a compatible JVM. 

The Javac compiler ensures that your code is free of any syntax errors and checks mistakes at the early stage of coding.

Understanding Javac and Its Purpose

The compilation process consists of several steps:

Lexical Analysis: Here the Javac compiler breaks the code into separated tokens, keywords, literals and operators.

Syntax Analysis: The compiler verifies that the tokens form valid Java syntax according to the language grammar.

Semantic Analysis: The compiler performs type-checking and other validations to ensure that the code adheres to the Java language rules.

Code Generation: The compiler generates bytecode instructions corresponding to the Java source code.

Bytecode Output: The compiled bytecode is written to .class files, which the JVM can execute.

Also read, pwd command in linux

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Using javac to Compile Java Code

The basic syntax for using the javac command is as follows:

javac [options] [source files]


In the above code, “options” are optional flags that change the compiler's behaviour; as the name suggests, “source files” are the file you want to run. One more thing is you can run more than one source file at the same time.



Commonly Used Options

The Javac command provides a wide range of options that allow developers to customise the compilation process. Let's explore some commonly used options:

  • -d: Specifies the destination directory for the compiled bytecode files. It tells the path of the compiled bytecode. For example, using javac -d ./bin will place the compiled NinjaCode.class file in the ./bin directory.
  • -classpath or -cp: Sets the classpath, which is a list of directories and JAR files that the compiler uses to find other classes that your program depends on. The class path is crucial when working on projects involving multiple classes. For example, javac -cp ./lib/* includes all JAR files in the ./lib directory as part of the classpath.
  • -source: Specifies the Java source version you are using. This option is useful when working with different Java versions. For example, -source 11 informs the compiler that the source code adheres to Java 11 syntax.
  • -target: Specifies the version of the generated bytecode. This option allows you to compile for a specific JVM version, ensuring compatibility. For example, -target 11 generates bytecode compatible with Java 11.
  • -Xlint:option: Enables specific compiler warnings. For instance, using -Xlint: unchecked will warn about unchecked operations in your code, promoting safer programming practices.
  • -verbose: Prints additional information during the compilation process, such as the names of classes being compiled, aiding in understanding the compilation flow.

Also see,  Eclipse ide for Java Developers

Frequently Asked Questions

What is javac, and what does it do?

Javac is the command-line Java compiler included in the JDK. It converts human-readable Java source code into platform-independent bytecode, allowing it to run on any system with a compatible JVM.

How do I use the Javac command to compile Java code?

You can use Javac followed by optional flags (options) and the name of the Java source file (source files) you want to compile. For example, javac we used in the above example.

What are some commonly used options with Javac?

Commonly used options include -d  to specify the destination directory for compiled files, -classpath to set the classpath, and -source to specify the Java source version.

How can I handle multiple source files with Javac?

You can use wildcards or specify multiple source files when using the Javac command. For example, to compile all .java files in the current directory: javac *.java.

What are the best practices for using Javac effectively?

Some best practices include explicitly specifying source and target versions, enabling and checking compiler warnings, organising code into packages, and using a build system like Maven or Gradle for larger projects.


In this article, we have understood Javac with its explanation and example. We have also covered some commonly used Options in Javac. Below is the recommended article you must go through. It will help you to get more knowledge about Java and Javac.

Check out our latest courses and pick out the best one for you with the help of the course overview from coding ninja Studio.

Happy learning!

Previous article
ClassLoader in java
Next article
Live masterclass