0

I was writing a simple program to send requests to a certain api using java and i decided to use jetty-client for http request handling.I decided to use jetty-client-11.0.0 to do this but upon running a basic script I keep getting the java.lang.NoClassDeffoundError;

package currencyconverter;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;


public class urler{
    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException, IOException {
        HttpClient httpClient = new HttpClient();


        ContentResponse response = httpClient.GET("example.com");
        System.out.println(response.toString());

    }
}

then the error is;

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/util/component/ContainerLifeCycle
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1013)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at currencyconverter.urler.main(urler.java:12)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.util.component.ContainerLifeCycle
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 10 more

I tried changing the version of jetty-client but it didn't seem to work ie from jetty-client 12.0.0.alpha3 to jetty-client 11.0.0 thinking that it was a version error and hoping that it would fix the issue but it didn't work.

CrummY
  • 1
  • 2

1 Answers1

0

Welcome to the community. The reason for this error is, that one dependency is transitive and is not referenced directly. Either you need to include the dependency org.eclipse.jetty:jetty-util directly (see maven coordinates here: mavencentral), or you can use maven and use this pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.stackoverflow</groupId>
    <artifactId>stackoverflow</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-client</artifactId>
            <version>11.0.8</version>
        </dependency>
    </dependencies>

</project>

If you want to find out, if there are more transitive dependencies, and you have no maven, you would need to unzip the dependency jar files and check the META-INF folder. It contains the pom.xml which contains the necessary dependencies.

dependencies of jar

If you use already maven, you can use the command mvn dependeny:tree which will output all direct and transitive dependencies:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ stackoverflow ---
[INFO] com.stackoverflow:stackoverflow:jar:1.0-SNAPSHOT
[INFO] +- org.slf4j:slf4j-api:jar:2.0.5:compile
[INFO] +- org.slf4j:slf4j-simple:jar:2.0.5:compile
[INFO] \- org.eclipse.jetty:jetty-client:jar:11.0.8:compile
[INFO]    +- org.eclipse.jetty:jetty-http:jar:11.0.8:compile
[INFO]    |  \- org.eclipse.jetty:jetty-util:jar:11.0.8:compile
[INFO]    +- org.eclipse.jetty:jetty-io:jar:11.0.8:compile
[INFO]    \- org.eclipse.jetty:jetty-alpn-client:jar:11.0.8:compile
luckyluke
  • 491
  • 7
  • 16
  • So in eclipse i would have to manaully check if the dependency is transitive? because i don't have maven i just use eclipse and i reference the libraries – CrummY Dec 16 '22 at 09:52
  • Im sorry I am really new to java and I have never worked with maven before so could you please dumb down the answer a bit. – CrummY Dec 16 '22 at 10:06
  • you can convert your eclipse project to a maven project. Updating the answer would be to exhaustive. Please checkout the tutorials here: [link](https://www.vogella.com/tutorials/EclipseMaven/article.html) – luckyluke Dec 16 '22 at 10:11
  • Do you have any good documentation that can assist me to become more familiar with maven because i originally just used to use the "Add external archives" option in eclipse in order to use libraries but now I understand it is ineffective. So do you have any references that can help me to understand more about maven and what it really is. – CrummY Dec 16 '22 at 10:51
  • Maybe start with Maven in [5minutes](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html) and then move on to the [getting started guide](https://maven.apache.org/guides/getting-started/index.html) – luckyluke Dec 19 '22 at 08:22