简体   繁体   中英

Java.lang.NoClassDefFoundError: org/eclipse/jetty/util/component/ContainerLifeCycle

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM