运行码头时发生错误NoClassDefFoundError

时间:2019-10-25 14:47:38

标签: java maven jetty

我尝试在jar可执行文件中创建一个码头服务器 我有Maven 3.6.0和OpenJdk 11.0.4

我的第一次运行失败,启动代码时,我收到以下错误消息:

  

线程“ main”中的异常java.lang.NoClassDefFoundError:javax / servlet / http / HttpServletResponse           在com.my.test.Main.main(Main.java:13)       引起原因:java.lang.ClassNotFoundException:javax.servlet.http.HttpServletResponse           在java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)           在java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:178)           在java.base / java.lang.ClassLoader.loadClass(ClassLoader.java:521)           ...还有1个

代码:

import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {

    public static void main(String[] args) throws Exception {
        int port = Integer.parseInt(System.getProperty("port", "8080"));
        Server server = new Server(port);

        ProtectionDomain domain = Main.class.getProtectionDomain();
        URL location = domain.getCodeSource().getLocation();

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
        webapp.setServer(server);
        webapp.setWar(location.toExternalForm());

        server.setHandler(webapp);
        server.start();
        server.join();
    }
}

pom.xml依赖项:

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.22.v20191022</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>9.4.22.v20191022</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.4.22.v20191022</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-plus</artifactId>
            <version>9.4.22.v20191022</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>${version.jetty}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>apache-jsp</artifactId>
            <version>${version.jetty}</version>
        </dependency>
    </dependencies>
</project>

我不知道发生了什么,有人可以帮忙吗?

先谢谢您。

1 个答案:

答案 0 :(得分:0)

如果您要进行嵌入式码头,则服务器上的任何依赖项都不会标记为...

<scope>provided</scope>

服务器永远不会在其外部“提供”依赖项。

提供的依赖关系是战争打包项目的典型代表,表明服务器将在运行时为它们提供特定的依赖关系。