Creating jar files using Command Prompt

Aditya Rao
Geek Culture
Published in
4 min readJun 16, 2021
Photo by Maximilian Weisbecker on Unsplash

Introduction

Building jar files is supposedly an age old process which is basic/must to any programmer writing Java applications. The process has been made very easy with IDEs like Eclipse and IntelliJ. GUIs makes the menial tasks easy with a couple of clicks here and there.

However, it gets a little tricky when it comes to converting a basic application into a jar file using CLI ( command line interface) . I experienced this particularly when it comes to using Visual Studio Code Editor for writing java applications. So when the GUI doesn’t assist we have to go to the basics — aka CLI.

Setting Up a Basic Program for Execution

The first task would be to set up VS code to run a basic java application. All the implementations are done using CLI and VS code. To download VS code for writing a simple java application click here.

We need to set up extensions for running java applications on VS code. Microsoft’s Java Extension Pack provides 6 extensions ranging from language support and maven to Intellicode and auto-complete features. This extension can be installed from the extensions tab on the left panel in CLI.

The basic structure of the applications looks like the image below:

Please note that the folders lib, bin and resources are for the additional java libraries you want to include in the project, for compiled class files and Manifest.txt file respectively.

Compile the Program and Create Classpath

Compiling the program involves using the java compiler. Normally, the programs are auto-compiled and all the background jazz is hidden from the user when compiling and setting the program up for execution.

So the command which is used for initiating the compiling process is javac. The command for compiling the code is as follows :

The command specifies the folder and the file which contains the main method and the outputs location using the -d parameter.

Now the special case in this situation would be including the jar file in the application. I’ve added a basic jar file ojdbc ( Oracle JDBC) used for interacting with Oracle Database to the lib folder. The compile command should include the location of the jar file along with the location of the folder to compile. The command for compilation changes and is as follows:

Creating Jar file from Compiled Project

For creating the jar file, we first need to create the Manifest.txt file. The manifest file is created under the resource folder in the project structure. The file looks like the one below:

The first line always has to be the version for Manifest. Then comes the class-path and main class. Class-Path should include all the files to be added to the jar including the jar files. Main-Class specifies the main method file in the project.

The command for creating jar is :

The command jar cvfm means create a jar , be verbose, specify the output file name and specify the manifest file name. You can see all the things there in the following arguments. ‘-C’ is to provide location for all classes.

Conclusion

While jar generation in usual scenarios is basically a click of the button ( or in case of maven and Dev-Ops pretty automated), having the knowledge of the basic commands which go into building a jar provides more perspective into the mechanics that work behind the scenes.

** This article is part of a small series of articles in regards to the process I underwent trying to figure our container building and execution. So stay tuned for more on my exploration journey as I do it.

--

--

Aditya Rao
Geek Culture

In between an analyst, scientist and programmer. Love stats, finance, ML, and perspectives. Currently interested in WebServices