selenium: NoClassDefFoundError after bumping to selenium-java 4.0.0 in Mac OS

🐛 Bug Report

I have a couple of repositories using Selenium 4:

I use GitHub Actions to run the tests in these repositories, using three different virtual environments:

  • ubuntu-latest (Ubuntu 20.04).
  • windows-latest (Windows Server 2019).
  • macos-latest (macOS 10.15).

After bumping to selenium-java 4.0.0-rc-1, several tests of the Mac OS workflow are failing due to NoClassDefFoundError, such as:

java.lang.NoClassDefFoundError: Could not initialize class org.openqa.selenium.WebDriverException
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:571)

… or:

java.lang.NoClassDefFoundError: Could not initialize class org.openqa.selenium.net.HostIdentifier
	at io.github.bonigarcia.wdm.test.create.ChromeRemoteTest.setupClass(ChromeRemoteTest.java:41)

To Reproduce/Test script

I don’t have a Mac OS machine to test it, but the error seems consistent in Mac OS (at least on GitHub Actions).

The test that fails are the following:

Environment

OS: macOS 10.15 (on GitHub Actions) Browser: Chrome Browser version: 93 Browser Driver version: chromedriver 93.0.4577.15 Language Bindings version: selenium-java 4.0.0-rc-1 Selenium Grid version (if applicable): 4.0.0-rc-1 (the dependency org.seleniumhq.selenium:selenium-grid is also used in WebDriverManager, but not in selenium-webdriver-java)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 17 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@bonigarcia @zhuyuemings I managed to reproduce the issue once, here is my stacktrace:

java.lang.NoClassDefFoundError: Could not initialize class org.openqa.selenium.net.HostIdentifier
	at integration.CollectionReloadingTest.reloadsCollectionOnEveryCall(CollectionReloadingTest.java:28)

Some error happens in the static initialisation block of class HostIdentifier. Unfortunately, I don’t see the exact error, and cannot reproduce it anymore. But it’s clear how to fix it: just add more try/catch blocks there.

P.S. As I already suggested in https://github.com/SeleniumHQ/selenium/issues/3048, the host resolving is not really needed and should be removed from Selenium code.

If things go well, we will release next week. You should join us in our Slack channel to stay even more up to date!

I resolved this problem by add “HostIdentifier.getHostAddress();” at the beginning of my program

I think this problem is due to the static code in HostIdentifier

static {
    // Ideally, we'd use InetAddress.getLocalHost, but this does a reverse DNS lookup. On Windows
    // and Linux this is apparently pretty fast, so we don't get random hangs. On OS X it's
    // amazingly slow. That's less than ideal. Figure things out and cache.
    String host = System.getenv("HOSTNAME");  // Most OSs
    if (host == null) {
      host = System.getenv("COMPUTERNAME");  // Windows
    }
    if (host == null && Platform.getCurrent().is(Platform.MAC)) {
      try {
        Process process = Runtime.getRuntime().exec("hostname");

        if (!process.waitFor(2, TimeUnit.SECONDS)) {
          process.destroyForcibly();
          // According to the docs for `destroyForcibly` this is a good idea.
          process.waitFor(2, TimeUnit.SECONDS);
        }
        if (process.exitValue() == 0) {
          try (InputStreamReader isr = new InputStreamReader(process.getInputStream(), Charset.defaultCharset());
               BufferedReader reader = new BufferedReader(isr)) {
            host = reader.readLine();
          }
        }
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
      } catch (Exception e) {
        // fall through
      }
    }
    if (host == null) {
      // Give up.
      try {
        host = InetAddress.getLocalHost().getHostName();
      } catch (Exception e) {
        host = "Unknown";  // At least we tried.
      }
    } 

but i don’t know what happens ~