Java Developer Kit contains tools which a Java developer needed to develop the Java programs, and JRE to run the program. In simple terms we can say that JDK is equal to JRE and Development Tools. The tools include Java Archive (jar), Java Compiler (javac), Java Disassembler (Javap), Java Debugger (jdb) , Java HeaderFile Generator (javah), Documentation (javadoc) and many others. We will look into some of the most used tool in this article.
Components of Java Development Kit
Java Archive (jar) :
Java Archive (jar) tool combines multiple Java files/ applets into a single archive .jar (Java Archive). jar is a archival and compression tool based on ZIP and ZLIB compression formats. Mainly jar is designed to package applets or multiple files into a single archive. Components of an applet or an application containing files,images, media elements etc once combined into a singe archive then the Java agent can download them in a single transaction rather than going for multiple transaction to get individual files. In addition the compression functionality reduces the download times.
Creating a Jar File
The syntax for creating a jar is
jar cf <<jar filename>> <<input file(s)>>
The options and arguments used in the above command are
- ‘c’ – This indicates that we want to create a jar file.
- ‘f’ – This indicates that we need the output to goto a file rather than sdout.
- jar filename – The name of the jar which needs to be created, the filenames are given with the extension of ‘.jar’ as a convention though it is not a mandatory.
- input files – List of the files which you want to put inside the jar file, multiple files can be added each separated by a space
Lets take the below two files Sample.java and Demo.java and put it in a single archive “Test.jar”
Sample.java
package com.javainterviewpoint; public class Sample { //Instance variable String name; //Parameterized Constructor public Sample(String name) { this.name = name; } public void show() { System.out.println("Hello "+name+"!!"); } public static void main(String args[]) { //Create object for Sample class Sample sample1 = new Sample("World"); //Call the show method sample1.show(); } }
Demo.java
package com.javainterviewpoint; public class Demo { public void disp() { System.out.println("Welcome to JavaInterviewPoint!!!"); } public static void main(String args[]) { Demo d1 = new Demo(); d1.disp(); } }
Upon executing the below command
jar cf Test.jar Sample.java Demo.java
we will have the “Test.jar” created.
Viewing the Contents of a JAR File
The syntax for viewing a jar is
jar tf <<jar filename>>
The options and arguments used in the above command are
- ‘t’ – This indicates that you want to view the contents of the jar file
- ‘f’ – This indicates that the JAR file whose contents are to be viewed is specified on the command line.
- jar filename – The name of the jar whose contents needs to be viewed
Upon executing the below command
jar tf Test.jar
We will get the below content displayed
META-INF/ META-INF/MANIFEST.MF Sample.java Demo.java
Extracting the Contents of a JAR File
The syntax for Extracting a jar is
jar xf <<jar filename>>
The options and arguments used in the above command are
- ‘x’ – This indicates that you want to extract the contents of the jar file
- ‘f’ – This indicates that the JAR file whose contents has to be extracted is specified on the command line.
- jar filename – The name of the jar whose contents needs to be extracted
Upon executing the below command
jar xf Test.jar
We will get the contents Sample.java and Demo.java extracted.
Updating a JAR File
The syntax for creating a jar is
jar uf <<jar filename>> <<input file(s)>>
The options and arguments used in the above command are
- ‘c’ – This indicates that we want to update an existing jar file.
- ‘f’ – This indicates that the JAR file whose contents has to be updated is specified on the command line.
- jar filename – The name of the jar whose contents needs to be updated.
- input files – List of the files which need to be added in addition
Upon executing the below command
jar uf Test.jar Sam.java
Now jar will be updated, Sam.java will be added along with Demo.java and Sample.java
Java Compiler (javac)
javac is the Compiler used in Java, javac tool it is location in the /bin folder of the JDK installation directory. Java Compiler reads all the class informations, interfaces and the other codes written in Java programming language and compiles them into byte code (.class file).
There are two ways to pass source code file names to javac
- For a small number of source files, list the name of the java files separated by space
-
javac <<file1.java>> <<file2.java>>
-
- For a large number of source files, create a file containing all the source file name separated by space or line breaks, to execute give the file name prefixed with @ symbol
-
javac @filelist
-
Java Disassembler (javap)
javap is also located in the /bin folder of the JDK installation directory. Java Disassembler disassembles one or more classes which is passed. The javap command prints the package, protected and public fields, and methods of the classes passed to it. The javap command prints its output to stdout.
Syntax
javap <<.class file1>> <<.class file2>>
Lets try to disassemble our Sample class
Output :
javap com.javainterviewpoint.Sample will give the below output
Compiled from "Sample.java" public class Sample { java.lang.String name; public Sample(java.lang.String); public void show(); public static void main(java.lang.String[]); }
Java Debugger (jdb) :
Java Debugger (jdb) is a command-line debugger for Java classes it helps us in debugging our Java code. In order to start debugging we simply need to give the class name after jdb
jdb <<class name>>
Once jdb is initialized we can give any of the jdb basic commands such as run, cont, print etc… You can get the complete list of jdb commands <– here. On running jdb over the above Sample code we will get output like below
jdb com.javainterviewpoint.Sample
Output :
c:\JIP>jdb Sample Initializing jdb ... > run run Sample Set uncaught java.lang.Throwable Set deferred uncaught java.lang.Throwable > VM Started: Hello World!! The application exited }
Java Header File Generator (javah) :
javah command generates C header and source files (.h file) which is needed to implement native methods. The generated header and source files can be used by C programs to reference an object’s instance variables from native source code. The package name is added in the header of the generated file, package name and Java Class name is seperated by underscore (_) delimiter.
Syntax
javah <<.class file1>> <<.class file2>>
Usually javah command creates a header file for each class listed on the command line and puts the files in the current directory.
When we run javah for our Sample class
javah com.javainterviewpoint.Sample
Sample.h file will be created in the current directory the content will look like below
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_javainterviewpoint_Sample */ #ifndef _Included_com_javainterviewpoint_Sample #define _Included_com_javainterviewpoint_Sample #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif
We can use the -o option to get the file in the different name and one more advantage is that if you are giving multiple class file all will be concatenated into this single file. Suppose we have another class called Demo.java
Running javah with -o option
javah -o Combined.h com.javainterviewpoint.Sample com.javainterviewpoint.Demo
will give output file name “Combined.h” and the contents like below like below
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_javainterviewpoint_Sample */ #ifndef _Included_com_javainterviewpoint_Sample #define _Included_com_javainterviewpoint_Sample #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif /* Header for class com_javainterviewpoint_Demo */ #ifndef _Included_com_javainterviewpoint_Demo #define _Included_com_javainterviewpoint_Demo #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif
Java Documentation (javadoc) :
javadoc tool parses the declarations and documentation comments in the Java source files and produces a corresponding set of HTML pages. It describes public, default and protected classes, interfaces, constructors, methods, and fields except anonymous inner classes. The javadoc tool can be directly called on a single or multiple files, however using the below three ways you can run the javadoc tool without specifying the file name.
- Giving the package name itself
- Using -subpackage option of javadoc
- Using * wildcard character in front of source filename (*.java)
Syntax
javadoc <<source file1>> <<source file2>>
Leave a Reply