Exception in Thread 'Main' Java.Lang.Noclassdeffounderror:

How can I solve java.lang.NoClassDefFoundError?

After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader (in this case java.net.URLClassLoader), which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.

Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.

For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.

Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDEs like Eclipse and IntelliJ IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.

Exception in thread main java.lang.NoClassDefFoundError in testng

Add the missing google-guice-core dependency in the pom.xml.

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>

or download the jar from here and add it to the classpath.

Exception in thread main java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap error using Selenium Java

This error message...

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap

...implies that the file com/google/common/collect/ImmutableMap might be corrupted or there is some incompatibility between the version of the binaries you are using.


Further you need to take care of a couple of things as follows:

  • Instead of storing the chromedriver.exe binary right under C:\ try to put it within a directory as C:\\BrowserDrivers\\chromedriver.exe and change the System.setProperty() line accordingly. So effectively, the line of code will be:

    System.setProperty("webdriver.gecko.driver","C:\\BrowserDrivers\\chromedriver.exe");

Also ensure that:

  • JDK is upgraded to current levels JDK 8u311.
  • Selenium is upgraded to current released Version 4.1.3.
  • ChromeDriver is updated to current ChromeDriver v100.0 level.
  • Chrome Browser is updated to current chrome=100.0 (as per chromedriver=100.0.4896.60 release notes).


Reference

You can find a relevant discussion in:

  • java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Java

Exception in thread main java.lang.NoClassDefFoundError:

org/apache/http/params/SyncBasicHttpParams class is under httpcore-4.1-alpha1.jar , which can be downloaded from

http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/httpcomponents/httpcore/4.1-alpha1/httpcore-4.1-alpha1.jar

In future if you need to find a jar for a class, then you can use this link, this is generally helpful:

http://www.findjar.com/

Why do I get Exception in thread main java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver?

This is just an assumption since the pom looks good so far. The part that's a bit suspicious is the jar plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
...

It looks like you later on try to execute the created jar file with java -jar? In that case all the dependencies you define in the pom will be missing. Either use the dependency-plugin to collect the dependency jar files and use the classpath option when running the jar or use the shade-plugin to create an uber-jar that will contain your classes as well as the dependencies.

Using your pom allowed me to start Chrome, so the dependencies look good. So I think the way you start it causes that exception.

Update: since you use this setup to automate chrome, the shade plugin seems the best way to go. I am able to start chrome with this pom and main class in src/main/java

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>HotRouter</artifactId>
<version>1.0-SNAPSHOT</version>

<name>HotRouter</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.example.StartMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Main-Class

package org.example;

import org.openqa.selenium.chrome.ChromeDriver;

public class StartMain {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:/Program Files/Google/Chrome/Application/chrome.exe");

ChromeDriver driver = new ChromeDriver();
}
}

Then mvn package and java -jar ./target/HotRouter-1.0-SNAPSHOT.jar opens chrome. Your config is almost the same (I think only the phase config for the shade plugin is missing)

Its important to have the shade plugin within the <plugins> section of the pom, as it is not part of the default jar life-cycle. The <pluginManagement> section is just there to configure defaults for versions and configuration. See pom reference. So additional plugins will not be automatically enabled if only in that section.

Exception in thread main java.lang.NoClassDefFoundError

You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run

java pack.sample

(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)

If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:

javac -d classes Sample.java

or

javac -d classes src/pack/Sample.java

will (in both cases) create

classes/pack/Sample.class

You could then run

java -cp classes pack.Sample


Related Topics



Leave a reply



Submit