What is a java jar ?why do we need it ?
JAR stands for Java ARchive. A group of java classes and supporting files combined into a single file compressed with ZIP format and given .jar extension.
It is easy to use, handle, create, maintain, update.
If we are using a JAR file then we need to download only one single file hence it reduces the download time since we don't need to download multiple files.
It is basically used to compress the size of the files. Suppose we are developing a software that contains many files so we need a tool to compress the size of this files.so the JAR tool is used to compress the size.
If we want to share your code or software to others, no need to share all java files instead of sharing all files we can just share a single jar file with them.
You can run JAR files as programs by loading them into Java runtime. Packages, based on their purpose, can be run as an executable from the command line, as a desktop application, or as a library as part of a bigger Java application.
How to generate a jar file in Intellij?
We can generate jar files by using different IDEs. In this blog we are going to generate jar file by using Intellij.
Open your project for which you want to create a jar file.
You should have a TestNG xml file and test runner class for your project to generate a jar file.
Note:- we can invoke a jar file for one class only. If you want to run single class, you no need to create test runner and testing.xml file. In our projects we need to run multiple tests but we cant invocate a jar file for multiple classes, so test runner is required to trigger testng.xml file. TestNG xml file is required to run multiple tests.
Go to file option which is in the top left of your Intellij window.
Then click on project structure.
When your project structure window is opened you can select Artifacts in project Settings.
Then you should click on add(+) button.
Mouse hover on JAR, then you can select “from modules with dependencies…”.
Then create JAR from modules window will appear.
In main class you should browse your TestRunner class.
Then you should browse your resources package in “Directory for META-INF/MANIFEST.MF” and also you should select ‘include tests’ button to include your all tests.
Now click on ‘ok’ and ‘Apply’.
Then click on build section which is present in top left of your Intellij window.
Then select build artifacts. When you select build artifacts, build option should be displayed in action dialogue box.
You can wait until build action to be complete.
Now you can see that one jar file is created in the test-output and also one MANIFEST file should be created in resources package.
How to run jar file in command prompt?
Go to your jar file location in your project.
Open the command prompt in your jar file location.
Run the command java –jar ‘jar_file_name’.jar.
Then your project will start executing.
How to run jar file by just double click action?
Go to your jar file location in your project.
Create a new text file and open it.
Write “java –jar ‘jar_file_name’.jar” in that file.
Save the file name same as your jar file name and give extension .bat
Now just double click on that bat file then your project will start executing.