java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap while using WebDriver with Maven Dependencies in Java Selenium
Asked Answered
D

2

7
  1. It is portion the code after driver=new ChromeDriver(); line it give me error i check with sysout, errors are in the 2, well i am not well experienced using Maven but i am checking my pom.xml file , i gave Selenium dependencies.

-Any advice?, any helps appreciated

public WebDriver initilizeDriver() throws IOException
{
    Properties prop= new Properties();
    FileInputStream fıs=new FileInputStream("C:\\Users\\Melih Sancak\\my-amazonTest\\src\\main\\java\\com\\ObjectRepisotary\\app\\data.properties");
    prop.load(fıs);
    String browserName =prop.getProperty("browser");
    System.out.println(browserName);
    if(browserName.equals("chrome"))
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Melih Sancak\\Downloads\\chromedriver.exe");
        driver=new ChromeDriver();
    }
}

2. Error:

java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
    at org.openqa.selenium.remote.service.DriverService$Builder.<init>(DriverService.java:253)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.<init>(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
Dynatron answered 4/5, 2018 at 10:10 Comment(4)
add your maven pom.xml, atleast dependencies.Cuttle
added actually <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.11.0</version> </dependency> ,it has already in my .pom file , there is no syntax error in my code also.Dynatron
Are you sure that browserName equals "chrome" ?. Remember about case-sensitivity. Change to "String browserName =prop.getProperty("browser").toLowerCase();". And this should be void, your code doesn't return WebDriver. And you don't handle other browsers.Cuttle
No it is not the msitake , i printed out, it enters the loop.driver=new ChromeDriver(); this line i think the problem occur i syssout line before after then.it printed out before but not afterDynatron
G
8

The reason for this problem is guava library.

This problem got solved by adding the guava library in maven pom.xml

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.0.1-jre</version>
    </dependency>

One of my friend was also facing this issue and adding this library took care of it as that method ImmutableMap comes from guava

Pasting the error message as well so people will land here as many will have same problem

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lcom/google/common/collect/ImmutableMap;

at org.openqa.selenium.chrome.AddHasCasting.getAdditionalCommands(AddHasCasting.java:38)
at org.openqa.selenium.chrome.ChromeDriver$ChromeDriverCommandExecutor.getExtraCommands(ChromeDriver.java:123)
Gingrich answered 3/2, 2022 at 10:49 Comment(4)
I have the same NoSuchMethodError in selenium 4.1.2, but not in 4.1.1Streeter
I've created an issue at github.com/SeleniumHQ/selenium/issues/10324Streeter
Many thanks Gaurav!! your solution with the guava library solved my problem after several hours of unsuccessful debugging!Argile
Make sure to not include the <type>bundle</type> part in the snippet, that gave me a "Missing artifact com.google.guava:guava:bundle:31.1-jre https://mcmap.net/q/492509/-errors-in-pom-xml-with-dependencies-missing-artifactGrievance
K
3

The file com/google/common/collect/ImmutableMap might be corrupted:

Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature)

If you are using eclipse and want to check whether this file is corrupted just try to open it. It is located in the guava maven dependency. If it is corrupted it will show you invalid LOC header (bad signature).

Then locate the locate the .m2 folder, search for the corrupted file and delete it. Finally run a maven update.

That solved the problem for me.

Koffman answered 18/5, 2019 at 10:40 Comment(2)
Hi, I have the same problem but I don't have the folder com/google/common. Which dependency am I missing?Rew
Hello Jefe infiltrado! It is a bit hard to find out what is the issue with the information provided. Could you please share what error you facing? Perhaps, you might consider posting a new question.Koffman

© 2022 - 2024 — McMap. All rights reserved.