javac compiler tutorials with examples in java| Javac command
In this blog post, We are going to learn the java compiler command with tutorials and example usage.
Java compiler - Javac tool
Javac
is a java compiler tool
developed by Sun framework and now it is owned by Oracle. Java compiler used to read the java program and compile java program and convert into machine-readable bytecode.
Javac
tool is shipped as part of JDK software installation and location is in the bin
folder of JDK installed folder.
This tool is a basic and important tool to learn for java programming developers.
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 java programs.
Here is a command usage.
javac {options} {sourcejavafile} {commandlinearguments}
Three parameters are passed. Options
: in javac are standard options provided by the JDK framework source java file
: is a java program to read and compile command line arguments
: are arguments passed to the java program at the command line which read by the 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 First.java
First.java is read by the java compiler and complies into First. Class and place this file in the current directory.
Frequent important options for java coder.
How to set Classpath with javac in java?
Usually, javac
with the classpath
option is configured If there is any dependency of class for the compiled java class.
classpath
is the environment settings configuration value that is used by the java runtime system to find the classes and other java related objects when java programs are running. CLASSPATH
is configured at machine level as well as inline classpath settings. At the machine level, we can configure using environment variable CLASSPATH, and inline classpath settings are configured by supplying the -cp
or -classpath
option to JDK tools like javac.
We can set the classpath in javac with classes as well as a jar file. Configuring javac classpath in windows and javac classpath in Linux environments are the 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 the 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 the current directory, we have to provide source
path options to javac.
C:\javac -sourcepath c:\javawork\javafiles;.; MyFirstProgram.java
javac source option:
javac can also be used to compile the java programs with supporting lower versions of the JDK version using the -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 flexible to have a lower version of Java releases for java source only.
C:\javac -source 1.4 MyFirstProgram.java
javac target option:
By using the target
option in javac
, compiled classes are compatible with 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.12
), javac.exe is available in the d:\\jdk1.12\\bin
location. Suppose you are running the javac
command from a different directory say d:\\code
. javac First.java
command gives an error “Javac’ is not recognized as an internal or external command, operable program or batch file” in java.
javac
should be 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
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 java 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
path=%path%;.;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 an error that used to be encountered during Java learning.
C:\Work>javac MyFirstProgram.java
javac: file not found: first.java
Usage: javac
use -help for a list of possible options
When we compile a java file with the javac command, the above error is thrown in the system console.
javac is working fine no issues with javac settings.
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 the java file is not there, give the complete path of the java as input to the javac compiler.
Hope it works and compiles fine.
Conclusion
To Sump Up, You learned an important tool in the java language. java compiler with options and possible errors.