Understanding javac, java and jar tools in JDK
- Details
- Written by Nam Ha Minh
- Last Updated on 08 November 2021   |   Print Email
- Java compiler: javac
- Java application launcher: java
- Java archive tool: jar
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.- Compile: javac HelloWorld.java
- Run: java HelloWorld
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.javaUsing the javac tool, you compile these .java files into the following .class files:
Painter.class, Shape.class and Canvas.classThen 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.classThe result is a jar file created: Painter.jarThe 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.jarType 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.
- 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