使用Maven依赖的Java Selenium出现NoClassDefFoundError错误求助
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap in Selenium + Maven Hey there, let's work through this error you're hitting when initializing ChromeDriver! That NoClassDefFoundError for ImmutableMap almost always boils down to a missing or conflicting Guava dependency—since Selenium relies heavily on Google's Guava library for utility classes like this one. Even if you added the Selenium dependency, Maven might not be pulling in the right version of Guava, or there could be a conflict with another dependency in your project.
Here's a step-by-step breakdown to fix this:
1. Check Your Maven Dependency Tree
First, let's see what's going on with your dependencies. Run this command in your project root directory:
mvn dependency:tree
Look for entries with com.google.guava:guava. If you don't see it at all, that's the problem—Selenium's transitive dependency on Guava isn't being pulled in. If you do see it, check if there are multiple versions listed (that means a conflict).
2. Add Guava Directly to Your pom.xml
To eliminate any ambiguity, explicitly add the Guava dependency that matches your Selenium version. For example:
- If you're using Selenium 4.x (like 4.15.0), use a recent Guava version:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.1.3-jre</version> </dependency> - If you're on Selenium 3.x, stick to an older compatible version like
25.0-jre.
Make sure this is added alongside your existing Selenium dependency in pom.xml.
3. Clean and Rebuild Your Maven Project
Corrupted or outdated dependencies in your local Maven cache can cause this too. Run these commands to refresh everything:
mvn clean mvn install
This will wipe old build artifacts and re-download all fresh dependencies.
4. Refresh Your IDE's Maven Dependencies
Sometimes your IDE (IntelliJ, Eclipse, etc.) doesn't automatically pick up new dependencies. Right-click your project and select:
- IntelliJ:
Maven > Reload Project - Eclipse:
Maven > Update Project
Quick Side Note: Fix Your File Path
I noticed your FileInputStream uses single backslashes (\) in the path—Java interprets these as escape characters, which will cause an error later. Use double backslashes (\\) or forward slashes (/) instead:
FileInputStream fis = new FileInputStream("C:\\Users\\Melih Sancak\\my-amazonTest\\src\\main\\java\\com\\ObjectRepisotary\\app\\data.properties");
Following these steps should resolve the ImmutableMap missing class error. Let me know if you run into any snags with the dependency tree or version matching!
内容的提问来源于stack exchange,提问作者Melih Sancak

