What is JAR ?
JAR(Java ARchive) files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a
.jar file extension.
In simple words, a JAR file is a compressed format of a compiled Java project. This file contains with
.class files, metadata and some other resources. Also, these files are a way to distribute our source code and can be used by external programs.
Here we are focusing on how to run a
.jar file from the command line without using any IDE.
The first step should be we are going to need a java project to create a
.jar file and I’ll use one of my previous program which I used to run with selenium and TestNG.
You can refer to my article on “Use of .properties file in selenium project with TestNG” and I’ll take the same code and continue it further.
For your reference find my GitHub project here
Contribute to mwickramanayaka/Daraz-Add-to-card-test-Selenium development by creating an account on GitHub.
Since we were calling
main() from the PropertiesFileTestNG class we won’t be able to run the actual test in
main()the method class. For that, I have pasted this code in the class level in PropertiesFile Class.
//setting expected title from the ebay.com/Daraz.lk
static String expectedTitleebay ="Electronics, Cars, Fashion, Collectibles & More | eBay";
static String expectedTitledaraz ="Online Shopping Sri Lanka: Clothes, Electronics & Phones | Daraz.lk";//create the htmlReporter objectstatic ExtentSparkReporter htmlReporter;
static ExtentReports extent;
static ExtentTest test1;public static String browserName = null;
Copy all the above codes to your project and run it.
This will go to the site www.ebay.com and then it will validate the site by comparing the Title. After will enter “mobile” in the search box and hit enter.
I have used
@Testannotation to execute the test case which is test1 and used extent Reports as the reporting library.
After a successful test, it will call the
PropertiesFile and write the status of our test to the
So in the above code, we had to change the browser name to “chrome” or “firefox” by opening the project. So the same thing can be done with CMD without using Eclipse.
So First let's create a
.jar file by right-clicking on the project and going to the export option.
You will see this window and click Runnable JAR file.
Under Launch Configuration select the class in which you included the
main() method. In our case it is
Give a name to the JAR file (in my case I named it as
selenium.jar ) and define the export Destination and Finish.
Go to the physical location that you have saved the JAR file and you will see it created there as
Now we have created our JAR file and let's run it from the command line.
Open the cmd and navigate to the
selenium.jar file location.
Enter this command to execute the
selenium.jar file and hit Enter.
java -jar selenium.jar
You will see the progress in the cmd and passing all tests.
You will see the
SOP() outputs we print in the console and its passing. Also, we can go to extent reports and see the test results.
In this article, we created a JAR file and ran a selenium TestNG framework using the command line without using any IDE.