Search code examples
javamavenseleniumselenium-chromedriver

ChromeDriver works from IDE, but not from a jar


So, when I try to use a chromedriver from a jar file, I get an exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/chrome/ChromeDriver
    at SelJarTest.main(SelJarTest.java:13)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.chrome.ChromeDriver
    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)

However, when I run the program from an IDE (IntelliJ CE), it works just fine.

Here's my Maven dependencies:

        <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openqa.selenium.chrome.ChromeDriver</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

And my code:

public class SelJarTest
{
    private static final String WEB_DRIVER_PROP = "webdriver.chrome.driver");
    public static void main(String[] args)
    {
          String oldProp = System.getProp(WEB_D_PROP);
          System.setProp(WEB_DRIVER_PROP, "chromedriver" + File.seperator + "chromedriver.exe");
          ChromeDriver cd = new ChromeDriver();

          System.out.println("Enter stuff: ");
          int userin;
          Scanner scan = new Scanner(System.in);
          while((userin = Integer.parseInt(scan.nextLine())) < 9)
          {
              if(userin %2 == 0)
                  cd.navigate().to("https://www.youtube.com");
          }

          scan.close();
          cd.close();
          System.clearProp(WEB_DRIVER_PROP);

          if(oldProp != null)
             System.setProp(WEB_DRIVER_PROP, oldProp);
          System.out.println("Done.");
    }
}

For a little more clarity, the chromedriver.exe is in a folder called chromedriver, which is in the same directory as the jar:

DIR> chromedriver/chromedriver.exe
     SelJarTest.jar

Nothing I've found online seems to help resolve this. What have found that was similar to the error, was a bunch of "Oh, you need such and such dependency, or such and such file." I am wanting to avoid the use of environment variables, so that my co-workers might be able to run the program if I put the file on a server for them to use.

Any help is much appreciated.


Solution

  • I found a solution to my plight. I wasn't creating the jar file with everything I needed.

    I'm using IntelliJ as my IDE, and I should have been creating the Jar artifact from modules with dependencies. Project Structure > Artfacts > Add > JAR > from modules with dependencies...

    My dependencies ended up being:

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.26.0</version>
        </dependency>
    </dependencies>