Thoth
Thoth

Reputation: 1041

Run java Selenium in command prompt

I have this code:

import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
    public static void main(String[] args) {
        try{
            FirefoxDriver driver = new FirefoxDriver();
            driver.get("http:\\www.yahoo.com");
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

and I want to run it in cmd. For this reason I call the following commands in a .bat file.

javac -classpath "C:\selenium-2.42.2\selenium-server-standalone-2.42.2.jar;C:\selenium-2.42.2\selenium-java-2.42.2.jar;C:\selenium-2.42.2\selenium-firefox-driver-2.42.2.jar" Test.java
java Test

The following error is returned:

C:\selenium-2.42.2>javac -classpath "C:\selenium-2.42.2\selenium-server-standalo
ne-2.42.2.jar;C:\selenium-2.42.2\selenium-java-2.42.2.jar;C:\selenium-2.42.2\sel
enium-firefox-driver-2.42.2.jar" Test.java

C:\selenium-2.42.2>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/f
irefox/FirefoxDriver
        at Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.firefox.Firefox
Driver
        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)
        ... 1 more

Also, the following information will help:

  1. java -version got:

    java version "1.8.0_05" Java(TM) SE Runtime Environment (build 1.8.0_05-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

  2. C:\Program Files (x86)\Mozilla Firefox>firefox.exe -v | more Mozilla Firefox 30.0

Maybe this post will marked as duplicate but I followed others suggestions without any success. Can you please cast some light the situation?

Here is the jar files I use.

Thanks!

PS: Win7 64bit

Upvotes: 0

Views: 2638

Answers (1)

SiKing
SiKing

Reputation: 10329

Your first command javac builds the classes, but does not embed the dependencies into the final jar file, which is what the error is telling you: java.lang.NoClassDefFoundError. You still need to provide the same dependencies when you run the class.

javac -cp "C:\selenium-2.42.2\selenium-java-2.42.2.jar" Test.java
java -cp "C:\selenium-2.42.2\selenium-java-2.42.2.jar" Test

The selenium-java.jar should be enough for your case. Have a look at the graphic here http://www.seleniumhq.org/download/maven.jsp to see how the different selenium jars are contained in each other.

I do not believe you can use just javac alone to embed the dependencies into the final .jar. You will need other tools.

Upvotes: 2

Related Questions