javac compiler tutorials with examples in java

In this blog post, We are going to learn the java compiler command with tutorials and example usage

Java compiler - Javac tool

Java Compiler - javac tool

Javac is java compiler tool developed by Sun framework. Java compiler used to read the java program and compiles this java program machine-readable bytecode. Javac tool is shipped as part of JDK software installation and found in the bin folder of JDK installed folder. This tool is mostly a basic tool to learn for java programming developer. This article talks about the javac tutorials and examples with known issues with javac for the developers who are new to java.

javac command usage

javac is a command in java used to compile the java programs, there are three parameters are passed.

javac {options} {sourcejavafile} {commandlinearguments}

Options in javac are standard options provided by JDK framework
source java file is java program to read and compile
command line arguments are arguments passed to the java program at command line which read by java program at runtime using argument String[] args of main() method.

javac command line options in java

Following are the all options for javac command using -help command

C:\work>javac -help  
Usage: javac    
Where possible options include:  
  -g                         Generate all debugging info  
  -g:none                    Generate no debugging info  
  -g:{lines,vars,source}     Generate only some debugging info  
  -nowarn                    Generate no warnings  
  -verbose                   Output messages about what the compiler is doing  
  -deprecation               Output source locations where deprecated APIs are used  
  -classpath           Specify where to find user class files and annotation processors  
  -cp                  Specify where to find user class files and annotation processors  
  -sourcepath          Specify where to find input source files  
  -bootclasspath       Override location of bootstrap class files  
  -extdirs             Override location of installed extensions  
  -endorseddirs        Override location of endorsed standards path  
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.  
  -processor [,,...]Names of the annotation processors to run; bypasses default discovery process  
  -processorpath       Specify where to find annotation processors  
  -d              Specify where to place generated class files  
  -s              Specify where to place generated source files  
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files  
  -encoding        Specify character encoding used by source files  
  -source           Provide source compatibility with specified release  
  -target           Generate class files for specific VM version  
  -version                   Version information  
  -help                      Print a synopsis of standard options  
  -Akey[=value]              Options to pass to annotation processors  
  -X                         Print a synopsis of nonstandard options  
  -J Pass  directly to the runtime system

Compile java file using javac:-

D:\work>javac is read by java compiler and complies into First. Class and place this file in current directory.
Frequent important options for java coder.

How to set Classpath with javac:-

usually, javac with classpath settings is configured to have a dependency of class for the compiled java class.
classpath is environment settings configuration value that is used by java runtime system to find the classes and other java related objects when java programs are running. Usually, CLASSPATH is configured at machine level as well as inline classpath settings. At machine level, we can configure using environment variable CLASSPATH and inline classpath settings are configured by supplying -cp or -classpath option to JDK tools like javac.
We can set the classpath in javac with classes as well as a jar. Configuring javac classpath in windows and javac classpath in Linux environments are same.

javac classpath example

C:\javac -classpath .;d:/project or   
C:\javac -cp .;d:/project  

we can also set classpath at the command prompt using the below command. javac compiled directory is a current directory in the above example.

C:\set CLASSPATH=.;pathtodirectoryforclasses or jars;

classpath value should be either current directory (representing .) or any other directory which points to .class files, jar and zip files.
classpath default settings are the current directory. if we set CLASSPATH using javac -classpath command line options override the default directory.

javac sourcepath option:-

Whenever Java programs are compiled, javac looks for the source of java files in the current directory from where javac is executing. if javac wants to search for the java source files other than current directory, we have to provide source path options to javac.

C:\javac -sourcepath c:\javawork\javafiles;.;

javac source option:-
javac can also be used to compile the java programs with supporting lower versions of JDK version using -source option. With this option, we can always configure the options to have the java source code compatibility for the desired java releases. This option is flexibility to have a lower version of Java releases for java source only.

C:\javac -source 1.4

javac target option:- By using target option in javac, compiled classes are comparable to the JDK versions releases. Once .class files are generated with this option, those are comparable to particular JVM only
Javac Known issues for basic java programmer:-

For the first time user, when we are compiling a java program, we used to get a lot of issues. I am listing down the two important issues that I have faced while learning java
Fix for Javac’ is not recognized as an internal or external command, operable program or batch file issue in java:-

javac is the tool available JDK software, once JDK is installed say(d:\jdk1.6), javac.exe is available in d:\jdk1.6\bin location.
Suppose you are running javac command from different directory say d:\code, with command as javac, it will give error “Javac’ is not recognized as an internal or external command, operable program or batch file” in java.
javac should available in the directory where java files are compiling.
We can fix this in many ways by using inline path settings or environment variable settings.

javac -classpath settings:-

d:\code\ set path=%path%;.;d:\jdk1.6\bin;
Configure environment variable path for java in windows:-
when java is installed, windows does not know where javac is located, we have to set environment variables path pointed to the bin folder.
Go to My Computer, and right click on it and select properties, go to advanced tab, add Path environment variable which points to the location of d:\jdk1.6\bin
Once these settings are configured, we can able to compile in windows system from any location

Fix for javac: file not found: issue in java

javac: file not found” is one other error that used to get for basic java developer. This error is

javac: file not found:  
Usage: javac    
use -help for a list of possible options

When we compile a java file with javac command, the above error is thrown in the system console. javac is working fine no issues with javac settings.
But the possible solution is that there is no java file located in the current directory.
The solution is to find the java file in the directory using dir command in windows or ls command with Linux, if java file is not there, give the complete path of the java as input to javac compiler. Hope it works and compiles fine.

Please leave a comment if you like this.


Hi How do I use the javac -Werror option ? I try …

Kevin - Apr 2, 2012

Hi How do I use the javac -Werror option ?

I try compiling a file which has warnings from the command prompt in the below manner.
javac -FWerror

However no errors are displayed, it compiles fine.

Similar Posts