Artikel Terbaru

Saturday, 10 March 2012

javac compiler tutorials with examples in java

javac compiler

Java Compiler

Javac is java compiler tool developed by Sun framework. Java compiler used to read the java program and compiles this java program into machine readable byte code.Javac tool is shipped as part of JDK software installation and found in bin folder of jdk installed folder. This tool is mostly 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
sourcejavafile 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 First.java
First.java 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 are configured to have dependency of class for the compiled java class.
classpath is environment settings configuration value that are 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 classpath in javac with classes as well as 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 command prompt using the below command.javac compiled directory is 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 current directory. if we set CLASSPATH using javac -classpath commandline options overrides the default directory.
javac sourcepath option :-
Whenever java programs are complied, 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;.; MyFirstProgram.java
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 lower version of java releases for java source only.
C:\javac -source 1.4 MyFirstProgram.java
javac target option :- By using target option in javac, compiled classes are combatable to the jdk versions releases. Once .class files are generated with this option , those are combatable to particular jvm only
Javac Known issues for basic java programmer:-

For the first time user, when we are compiling 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 First.java, 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 doest not know where javac is located, we have to set environment variables path pointed to bin folder.
Go to My Computer, and right click on it and select properties, go to advanced tab, add Path environment variable which point to 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
javac: file not found: issue in java:-
"javac: file not found" is one of other error that used to get for basic java developer. This error is
C:\Work>javac MyFirstProgram.java
javac: file not found: first.java
Usage: javac  
use -help for a list of possible options

When we compile 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.