0

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

2
  • Try using WebDriver driver = new FirefoxDriver();. Commented Jul 2, 2014 at 15:49
  • With the same jar files, cmd returns similar error: the Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/W ebDriver. Strange again. Is anything wrond in jar file loading? Commented Jul 2, 2014 at 15:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Is there any difference between -classpath and -cp. In any case I run your suggestion instead of mine (second snippet) but cmd returns the same error after running java Test.
Sorry, got distracted in my first response. Corrected my answer.
Thanks for the response. I am running your snippet but I am get this error: C:\selenium-2.42.2>java -cp "C:\selenium-2.42.2\selenium-java-2.42.2.jar" Test Error: Could not find or load main class Test. Any ideas on that? By the way a Test.class file is created in the folder.`Sorry for my ignorance:)
You really should consider using a proper IDE, such as Eclipse or one of the others, that does all this work for you!
At last, the corerct command is: javac -cp "C:\selenium-2.42.2\selenium-java-2.42.2.jar;selenium-server-standalone-2.42.2.j‌​ar" Test.java and java -cp "C:\selenium-2.42.2\selenium-java-2.42.2.jar;.;selenium-server-standalone-2.42.2‌​.jar" Test.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.