Friday, April 10, 2020

Creating multipurpose executable Jar file

As the title says, this recipe is focused on creating a multipurpose jar file, where a single jar file can be used for multiple requirements. I am going to cover the whole process by diving into the following steps:
1) Create a Java application
2) Bundling into an executable Jar file
3) Running a Jar file

I take a problem scenario to explain the above steps and how they are carried out.

Problem:
We want to have a java application, which should have addition, subtraction, multiplication and division functionalities,  that we can call those functionalities providing the command line parameters. For example

add 2 4 4 (result=2+4+4)
subtract 54 12  (result=54+12)
multiply 12 54 55 (result=12*54*55)
divide 34 17 (result=34/17)


So, let's start:
1) Create a Java application
I am creating a Java application with Gradle as a build tool because it is easy to use. I am creating 4 classes with the main method which is responsible for corresponding operation and parameters. One option could be to create a single method and get an operation as a parameter, which makes it really complex to handle parameters. If we create a main method for each operation, it makes our tasks of handling parameters easy.

So, I begin with creating four java classes with main method, which manipulates the arguments to get the corresponding result. Because the example task is very simple, our actual task could be way bigger with many dependent libraries. So, we define a bunch of libraries in "build.gradle" file and we create a "fatJar" with all dependencies included in the jar file. An alternative will be we copy dependent libraries in the classpath.

I begin with the fatJar method, which create a jar file with all dependent libraries automatically from gradle. For this, we have to write a method to create fatJar. Have a look at the sample build.gradle file.

File: build.gradle



settings.gradle



The method jar create the jar file without dependencies. But the fatJar create a jar file with all dependencies, and we can use out of the box.

So, after definition of Gradle build information.

Now, we create class files with specific operation in main methods. The class files look something like this:

Add.java



Subtract.java



Multiply.java



Division.java



The application structure looks like this:



So far, we have created a java application with the required operations.


2) Bundling into an executable Jar file

Bundling into an executable Jar file is quite easy because we have already written a method in build.gradle file above. So, we just call

gradle clean && gradle fatJar

The created Jar file will be in folder build/libs as shown above(Operations.jar)

Note: If we simply run "gradle build", then it will create a thin jar file(i.e. without dependencies)


3) Running a Jar file

The Last Step is to execute the Jar file Operations.jar. The bundled jar file includes all the classes, and we can run all operations providing corresponding arguments.
For example:
java -cp Operations.jar com.kpaudel.operations.Add 12 34.33 45.3

Result=91.63

Similarly, we can check all other operations providing the parameters. 

java -cp Operations.jar com.kpaudel.operations.Subtract 123.56 23
Result=100.56

java -cp Operations.jar com.kpaudel.operations.Multiply 12.2 34.5
Result=420.9

java -cp Operations.jar com.kpaudel.operations.Division 12 2 2
Result=3.0


So, we can independently manage each operation with their own arguments, which makes very easy to handle input arguments. Apart from this advantage, bundling into a single jar file makes it easy to deploy, which means only a single jar file with many operations can be called separately.

I am using this method to create download and upload operations for my cloud storage, getting advantage of using the same jar file for different purposes of download and upload operations(with independent input parameters.) 


I hope you have enjoyed this recipe.  Thanks for reading :)