Understanding javac, java and jar tools in JDK
- Details
- Written by Nam Ha Minh
- Last Updated on 08 November 2021   |   Print Email
In the Java Development Kit (JDK), there are three tools which can be used in conjunction to develop Java applications. They are:
- Java compiler: javac
- Java application launcher: java
- Java archive tool: jar
This article gives you an overview about these 3 key tools in Java development.
1. Java compiler and Java application launcher
When you are using an editor or an IDE (Integrated Development Environment) like Sublime or NetBeans to develop Java applications, the editor/IDE invokes the java compiler (javac.exe) to compile the source files (.java) and calls the Java launcher (java.exe) to run the compiled files (.class). The following picture illustrates this process:
You can imagine that the javac and java are two engines of an airplane. Without engines, the airplane cannot take off. Missing one, the airplane cannot flight properly.
Technically, javac is the program that translates Java code into bytecode (.class file) - an intermediate format which is understandable by the Java Virtual Machine (JVM).
And java is the program that starts the JVM, which in turn, loads the .class file, verifies the bytecode and executes it.
javac and java are the cornerstones of the Java programming language. All editors or IDEs rely on these tools for compiling and executing Java applications.
So, how to get started?
It’s very easy. Here are the typical steps to compile and run a Java program whose source file is HelloWorld.java:
- Compile: javac HelloWorld.java
- Run: java HelloWorld
Of course there are parameters and options which you can find by typing javac -help or java -help.
Here are the official documentations for these tools:
Certainly it takes time and effort to control these tools. You cannot master them in just 1 or 2 days. Like a pilot has to experience thousands hours of flight in order to completely control the airplane.
Good news is that you can go faster with my experiences compiled into the following articles with practical examples:
2. The Java archive tool
Besides javac and java, there’s another one in JDK which is also an important tool in Java programming. It is the Java Archive tool - hence the jar name for its executable file.
Typically, jar is one of the tools that participate in the process of building a Java application, as shown in the following picture:
jar is a command-line tool just like javac and java. This tool is used for putting a group of related .class files into a single unit for easy deployment and execution. The result is a file created with .jar extension. The .jar file is actually a Zip file that contains all the .class files.
Therefore, Java applications and libraries are packed in .jar files, e.g. junit.jar, log4.jar, MyProgram.jar, etc.
You can think that a jar file is the executable of a Java application, like the .exe files on Windows and .sh files on Linux. When you double click the jar file, the operating system is triggered to start the JVM. In turn, the JVM loads and runs the application bundled in that jar file.
As a Java programmer, you need to understand and be able to use the jar tool for packaging your compiled code (.class files) to make deployments of Java libraries and applications in jar files.
For example, suppose that you did write a painter program in Java. It consists of 3 classes:
Painter.java, Shape.java, and Canvas.java
Using the javac tool, you compile these .java files into the following .class files:
Painter.class, Shape.class and Canvas.class
Then type the following command to put all these .class files into a single file named Painter.jar:
jar cf Painter.jar Painter.class Shape.class Canvas.class
The result is a jar file created: Painter.jar
The following picture illustrates the content of this jar file:
Now, you can execute this Java application using the java command with the -jar option:
java -jar Painter.jar
Type jar -help to see all available options for this tool.
For full syntax description, check the following official documentation from Oracle:
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/jar.html
Over the years, I compile my experiences with this tool in the following article:
You can find useful and practical examples about using the jartool in this article.
Here are some key points regarding the Java archive tool:
- Java libraries and applications are packed in jar files.
- A jar file is actually a Zip file. That means you can use any archive tools such as WinZip, WinRar, 7-Zip,… to view and extract content of a jar file.
- The jar tool provided by JDK is a command-line program that allows us to create, view and extract jar files.
- You can use the jar tool to extract a zip file or compress a file to zip format.
3. Conclusion
When you are working with the editor or IDE, they do all the interactions with javac, java and jar to hide the complex details and increase productivity.
That’s good! You can build Java applications without manually touching these tools. However, from my experience, I recommend you to study and play with these tools because of the following benefits:
- You understand the nuts and bolts of Java, which gives you experiences to solve problems related to compilation and execution of Java programs.
- You equip yourself with skills of working with or without IDEs. Sometimes, working directly with javac, java and jar saves you a lot of time than using an IDE, hence increasing your productivity.
- You have more control over Java programming, e.g. you can easily work with various tools that rely on javac, java and jar such as Ant, Maven, Gradle, etc.
My friend, today I want to tell you this: to master Java programming, you have to master its core: the Java compiler, Java launcher and Java archive tools. If you want to fly the airplane, you must be able to control the engines.
Other Java Tools Tutorials:
- javac command examples
- java command examples
- Java jar command examples
- Java serialver command examples
- The Java Shell (jshell) Tutorial
- How to compile, package and run a Java program using command-line tools (javac, jar and java)
Comments