Search code examples
javaawaitility

Awaitility - java.lang.NoClassDefFoundError thrown when trying to run the program from eclipse


Awaitility is used for synchronizing asynchronous operations. So I'm trying to use it for my automation project to handle the synchronization issues. So I tried with some basic program.

import static org.awaitility.Awaitility.*;
import static org.awaitility.Duration.*;
import static java.util.concurrent.TimeUnit.*;
import static org.hamcrest.Matchers.*;
import java.util.concurrent.TimeUnit;

 public static void main( String[] args )
        {        
            System.setProperty("webdriver.chrome.driver", "C:/Learning/synchronization/Resources/chromedriver.exe");    
            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("http://www.seleniumeasy.com/test/dynamic-data-loading-demo.html");
            WebElement newUserBtn = driver.findElement(By.id("save"));
         WebElement loadingElement = driver.findElement(By.id("loading"));

            // Get a new User
            newUserBtn.click();

            await().atMost(15,TimeUnit.SECONDS).until(loadingElement::getText, not("Loading..."));
            System.out.println("User profile retrieved");
        }

But whenever it encounters the below line, it throws the below exception

await().atMost(15,TimeUnit.SECONDS).until(loadingElement::getText, not("Loading..."));

Error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/awaitility/Awaitility
    at com.testing.automation.synchronization.App.main(App.java:35)
Caused by: java.lang.ClassNotFoundException: org.awaitility.Awaitility
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

And I thought of whether any dependencies got missed out which leads to the below error. But i'm not sure. And I'm using the maven project with Java 8 version.

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.awaitility/awaitility -->
        <!-- https://mvnrepository.com/artifact/org.awaitility/awaitility -->
        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.awaitility/awaitility-proxy -->
        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility-proxy</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

enter image description here

I really don't have a clue why i'm getting this error.
Could any one please help me out to resolve this issue?


Solution

  • In your maven pom you are importing the utility only in test scope. However, it seems you are trying to run the application in a normal mode not a test mode through using main.

    Try to remove <scope>test</scope> for the first two dependencies from the pom file.

    By the way, I did some reading and it looks like this utility is designed for tests only, not production.