0

I have a problem with implementing automatic chromedriver for Selenium unpacking from .jar file.

Having

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("resources/drivers/chromedriver.exe");
    File f = new File("Driver");
    if (!f.exists()) {
        f.mkdirs();
    }
    File chromeDriver = new File("Driver" + File.separator + "chromedriver.exe");
    if (!chromeDriver.exists()) {
        try {
            chromeDriver.createNewFile();
            org.apache.commons.io.FileUtils.copyURLToFile(resource, chromeDriver);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return chromeDriver.getAbsolutePath();

in one class, that returns the path to the executable driver, which is used in

System.setProperty("webdriver.chrome.driver", the_path);

Printing out path gives me obviously proper path to the driver file, and the file is being created under given directory right. Yet, while running .jar the following problem occures

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
(...)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        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)

but only while triggering the class that's using the driver.

Is there anything to be changed in the code itself or am I missing some crucial things in compilepath or something similiar?

Would appreciate any help!

Canert
  • 23
  • 6
  • The exception states that org.openqa.selenium.WebDriver cannot be found. This has nothing to do with chromedriver. – MikeJRamsey56 Aug 10 '17 at 19:05
  • Everything goes fine while being run from IntelliJ; I have added standalone server etc. yet still I receive something like that. Any suggestions? – Canert Aug 10 '17 at 19:25
  • [Set the classpath](https://test-able.blogspot.com/2014/11/run-web-driver-scripts-from-command-prompt.html). – MikeJRamsey56 Aug 10 '17 at 19:32
  • How to do that in IntelliJ? – Canert Aug 10 '17 at 19:48
  • You said that it ran fine in IntelliJ. The link I gave you shows how to run the same test on the command line outside of an IDE. Ignore the Eclipse stuff; IntelliJ has equivalent features. You can figure it out. :-) – MikeJRamsey56 Aug 10 '17 at 20:32
  • It's more like a program with a GUI etc. that I use as in need of logging into pages that HTMLUnit can't handle. Hope it won't change a thing... (?) – Canert Aug 10 '17 at 23:19
  • The problem is that your selenium jar is not in your classpath. That is what you have to fix. See for example this [SO Q&A](https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project). – MikeJRamsey56 Aug 11 '17 at 21:10
  • @Canert - Can you please tell us how you are building your code as a jar and how you are running it from the command prompt ? If you are basically exporting your code from the IDE as a jar, then the jars setup in your build path are not bundled in your jar. In those cases, you would need to specify the selenium standalone jar as part of your class path and then run your code. So it would be better if you can help answer these questions so that we can help you out. – Krishnan Mahadevan Aug 12 '17 at 03:33
  • I'm simply building an artifact in IntelliJ, having everything needed added to "dependecies" and "libraries" tabs in project structure menu. And then just run the resulting .jar via command prompt. And as a person focusing on a code, I have no idea how to add something to or change classpath. – Canert Aug 13 '17 at 22:00
  • Forgot earlier - the solution was simply to add libs folder to the project build ("include in project build"). – Canert Sep 05 '17 at 15:33

0 Answers0