Java.lang.NoClassDefFoundError:org/eclipse/码头/实用程序/组件/容器生命周期

c9x0cxw0  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(185)

我写了一个简单的程序,用java向某个API发送请求,我决定用jetty-client来处理http请求,我决定用jetty-client-11.0.0来做这件事,但是在运行一个基本的脚本时,我总是得到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());

    }
}

字符串
则误差为;

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

我尝试更改jetty客户端的版本,但似乎没有工作,即从jetty客户端12.0.0.alpha3到jetty客户端11.0.0,以为这是一个版本错误,并希望它会解决这个问题,但它没有工作。

jgwigjjp

jgwigjjp1#

欢迎来到社区。出现此错误的原因是,一个依赖项是transitive,并且没有被直接引用。您可以直接包含依赖项org.eclipse.jetty:jetty-util(请参阅此处的maven坐标:mavencentral),或者您可以使用maven并使用以下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>

如果你想知道,如果有更多的可传递依赖项,并且你没有maven,你需要解压缩依赖项jar文件并检查META-INFfolder,它包含pom.xmlpom.xml包含必要的依赖项。

如果你已经使用了maven,你可以使用命令mvn dependeny:tree,它将输出所有的直接和传递依赖:

[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

相关问题