Parvez Parvez - 2 years ago 119
Java Question

How to run a java program with selenium jar through command line

How to run a java program with selenium jar through command line...

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HelloSelenium {

public static void main(String[] args)
WebDriver driver;

driver = new FirefoxDriver();



I am getting following error on running it through CLI

java -cp ".;./jars/selenium-java-2.53.0.jar" HelloSelenium

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/bas
at HelloSelenium.main(
Caused by: java.lang.ClassNotFoundException:
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

Answer Source

If you specify the classpath explicitly, the current directory is not included by default. So you should do

java -cp selenium-java-2.53.0.jar;. HelloSelenium

Note the extra ";.", e.g. adding . to classpath.

This will still be insufficient, however, as selenium itself has many other libraries as dependencies (included in the libs folder of your download). You will need to add those to your classpath as well.

Easiest to add them all with a wildcard for your simple example. So you should use:

java -cp selenium-java-2.53.0.jar;libs/*;. HelloSelenium

Note the "libs/*", which assumes you are in the root folder of your selenium download.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download