How to run a Selenium Project from the command line using JAR files

Manul Wickramanayaka
May 24 · 4 min read

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

PropertiesFile Class:

PropertiesFile.java

PropertiesFileTestNG Class:

PropertiesFileTestNG.java

config.properties:

browser=chrome

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 .properties file.

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.

Export

Under Launch Configuration select the class in which you included the main() method. In our case it is PropertiesFile.class

Give a name to the JAR file (in my case I named it as selenium.jar ) and define the export Destination and Finish.

Runnable JAR File Export

Go to the physical location that you have saved the JAR file and you will see it created there as selenium.jar

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.

cmd progress

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.

Extent Report

In this article, we created a JAR file and ran a selenium TestNG framework using the command line without using any IDE.

THINK QUALITY!

Test Automation Hub

Test Automation Hub

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Start a blog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store