'How do I make my selenium imports work properly in Java 11 (Eclipse 2021-12) and fix "package cannot be resolved to a module"?
I am working on a project that requires both Selenium, as well as the StringUtils class from org.apache. I actually already completed this project before in Java 8, but I recently had to upgrade to Java 11. For relevant information, I am now using the Eclipse-IDE 2021-12 (for Java), and I am using JDK 11.0.14 (Java 11). This is not a Maven or a Gradle project (so I don't have any pom.xml). I do have a module-info.java file. I have added the following jars to my ModulePath:
byte-buddy-1.8.15.jar
client-combined-3.141.59.jar
client-combined-3.141.59-sources.jar
commons-exec-1.3.jar
guava-25.0-jre.jar
okhttp-3.11.0.jar
okio-1.14.0.jar
commons-lang3-3.12.0.jar
commons-lang3-3.12.0-javadoc.jar
commons-lang3-3.12.0-sources.jar
commons-lang3-3.12.0-tests.jar
commons-lang3-3.12.0-test-sources.jar
The project worked just fine with the above jars back when I was using Java 8 and an older version of eclipse. Java 8 did not use the whole module system, so all I had to do was click on "configure build path" and "add external jar", and the above jars would be added to the referenced libraries and my import statements would pose no problems.
However, now in Java 11, I seem to have to choose between adding these jars to my classpath or adding to my modulepath, and either way, my import statements cause errors. Here are my import statements:
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
Whenever I add the jars to my modulepath, the import statements create compiler errors and say "The package is not accessible". I can add the following statements to my module-info.java file:
requires okio;
requires org.apache.commons.lang3;
The packages okio and org.apache.commons.lang3 are packages within the okio-1.14.0 and the commons-lang3-3.12.0 jars respectively. When I add the above requires statements, the error on the StringUtils import goes away (the following import):
import org.apache.commons.lang3.StringUtils;
However, I also try to add the following require statement:
requires org.openqa.selenium;
I get the error "org.openqa.selenium cannot be resolved to a module". This is odd because "org.openqa.selenium" is a package within the client-combined-3.141.59 jar. Yet for some reason, the compiler acts like this package doesn't exist, and my import statements that are related to selenium say "The import cannot be resolved".
I cannot seem to add the jars to both the classpath and the modulepath at the same time, and when I try moving the selenium related jars to the classpath instead of the modulepath, the compiler suggests that I move okio-1.14.0 and client-combined-3.141.59 back to the modulepath. Doing so results in my selenium related import statements saying that the package is not accessible.
How do I deal with this issue? Is it just impossible to use selenium with Java 11?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
