Export Eclipse Java Project as Runnable JAR

Last modified on July 26, 2024

This tutorial explains how to create a runnable JAR file from an existing Selenium Eclipse project (Java). If you do not have an Eclipse project, see this tutorial instead: Create Runnable JAR From Selenium Script Using Eclipse.

1. Add Main Method

If you use JUnit to write and run your Selenium tests, you probably do not have a main() method in your code. But, AlertSite can only monitor runnable JARs, and a runnable JAR needs a main() method. Here is how you can add it.

First, edit your .java file and add these lines at the beginning of the file, after other imports:

Java

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

Then, add this main() method inside the class:

Java

public static void main(String args[]) {
  JUnitCore junit = new JUnitCore();
  junit.addListener(new TextListener(System.out));
  Result result = junit.run(SampleTest.class); // Replace "SampleTest" with the name of your class
  if (result.getFailureCount() > 0) {
    System.out.println("Test failed.");
    System.exit(1);
  } else {
    System.out.println("Test finished successfully.");
    System.exit(0);
  }
}

Next, run the code to verify how the main() method works. To do this, right-click the .java file and select Run As > Java Application.

Run as Java application

Click the image to enlarge it.

This will run your Selenium script: start a web browser, navigate to the tested web page, perform the test actions, and then close the browser. You should see Test finished successfully in the Eclipse console.

Test finished successfully

Click the image to enlarge it.

If an error occurs, fix the code to avoid the error.

2. Export Project as Runnable JAR

After you have confirmed the code works as expected, you need to export the Java project to a runnable JAR file so that it can be run by AlertSite.

To export your project, right-click it and select Export.

Export project

Select Java > Runnable JAR file as the export destination and click Next.

Export destination - Runnable JAR file

On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.

Also, be sure to select Package required libraries into generated JAR to embed Selenium libraries into your JAR file. JARs created without this option cannot be run by AlertSite.

Package required libraries into generated JAR

Click Finish to create the JAR file.

To confirm that the JAR file has been properly packaged, open the command prompt and run this command:

java -jar <path>\filename.jar

For example:

java -jar C:\Tests\AlertSiteSeleniumExample.jar

This will open the browser and run your Selenium script. You should see Test finished successfully in the console.

What’s Next

Now that you have a runnable JAR file containing your Selenium script, you can upload it to AlertSite to run your script from various locations around the world.

See Also

Website Monitoring With Selenium and AlertSite
Guidelines for Selenium Scripts

Highlight search results