0

I created a Maven project for Selenium automation. Am able to execute the test from Eclipse through "RunAs Configurations" as below screenshot:

RunAs Configuration window from eclipse

I am able to execute mvn clean and mvn package without any errors. But when I execute the command java -cp target\auto-0.0.1-SNAPSHOT.jar scripts.FirstMain, I am getting the error as below:

D:\SeleniumProject>java -cp target\auto-0.0.1-SNAPSHOT.jar
scripts.FirstMain
    Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/W
    ebDriver
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
            at java.lang.Class.getMethod0(Unknown Source)
            at java.lang.Class.getMethod(Unknown Source)
            at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 6 more
    D:\SeleniumProject>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.sample.test</groupId>
    <artifactId>auto</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>autoTrial</name>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.1</version>
        </dependency>
    </dependencies>
</project>

My code is :

package scripts;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstMain {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  WebDriver wd = new FirefoxDriver();
  wd.get("http://www.gmail.com");
  wd.manage().window().maximize();
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  wd.close();
  wd.quit();
 }
}
Eugene S
  • 6,709
  • 8
  • 57
  • 91
sampath
  • 33
  • 8

2 Answers2

0

You should add your lib directory too in the classpath. Do like this :

java -cp "target\auto-0.0.1-SNAPSHOT.jar;target\lib\*" scripts.FirstMain
S.K. Venkat
  • 1,749
  • 2
  • 23
  • 35
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
0

The exception is because of not finding your dependencies at run-time, one solution is to add that dependency to your class path or you can package your jar with all dependencies by using maven assembly plugin.link here

Community
  • 1
  • 1
Mr.Q
  • 4,316
  • 3
  • 43
  • 40