【发布时间】:2020-02-21 22:34:58
【问题描述】:
我尝试在 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>
我不明白发生了什么,有人可以帮忙吗?
提前谢谢你。
【问题讨论】:
-
谢谢你,但它没有用,我总是有同样的例外,无论我做什么。
-
缺少的类
HttpServletResponse位于 servlet-api 中。您是否尝试将其作为正常依赖项,并提供not?然后检查它是否最终在你的罐子里。