java ee:尽管导入有效,但未找到类?!
我使用jersey创建某种RESTful API。为了获得关于当前请求的信息,我使用了HttpServletRequest对象。我能够编译项目而不出现问题或错误,但是当我运行它时,我得到以下错误:java ee:尽管导入有效,但未找到类?!,java,maven,jakarta-ee,classnotfoundexception,jersey-2.0,Java,Maven,Jakarta Ee,Classnotfoundexception,Jersey 2.0,我使用jersey创建某种RESTful API。为了获得关于当前请求的信息,我使用了HttpServletRequest对象。我能够编译项目而不出现问题或错误,但是当我运行它时,我得到以下错误: [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ backend --- [WARNING] java.lang.reflect.InvocationTargetException at sun.reflect.NativeMet
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ backend ---
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
at java.lang.Class.getDeclaredMethods(Class.java:1855)
at org.glassfish.jersey.server.model.IntrospectionModeller$2.run(IntrospectionModeller.java:236)
at java.security.AccessController.doPrivileged(Native Method)
at org.glassfish.jersey.server.model.IntrospectionModeller.getAllDeclaredMethods(IntrospectionModeller.java:230)
at org.glassfish.jersey.server.model.IntrospectionModeller.checkForNonPublicMethodIssues(IntrospectionModeller.java:170)
at org.glassfish.jersey.server.model.IntrospectionModeller.doCreateResourceBuilder(IntrospectionModeller.java:118)
at org.glassfish.jersey.server.model.IntrospectionModeller.access$000(IntrospectionModeller.java:80)
at org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:111)
at org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:108)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
at org.glassfish.jersey.server.model.IntrospectionModeller.createResourceBuilder(IntrospectionModeller.java:108)
at org.glassfish.jersey.server.model.Resource.from(Resource.java:744)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:400)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:163)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:323)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:320)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:331)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:116)
at com.getbro.api.Main.startServer(Main.java:32)
at com.getbro.api.Main.main(Main.java:42)
... 6 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 36 more
[INFO]——execmaven插件:1.2.1:java(默认cli)@backend---
[警告]
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
运行(Thread.java:745)
原因:java.lang.NoClassDefFoundError:javax/servlet/http/HttpServletRequest
位于java.lang.Class.getDeclaredMethods0(本机方法)
位于java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
位于java.lang.Class.getDeclaredMethods(Class.java:1855)
位于org.glassfish.jersey.server.model.IntrospectionModeller$2.run(IntrospectionModeller.java:236)
位于java.security.AccessController.doPrivileged(本机方法)
位于org.glassfish.jersey.server.model.IntrospectionModeller.getAllDeclaredMethods(IntrospectionModeller.java:230)
在org.glassfish.jersey.server.model.IntrospectionModeller.checkfornonpublicmethoddissues(IntrospectionModeller.java:170)上
位于org.glassfish.jersey.server.model.IntrospectionModeller.doCreateResourceBuilder(IntrospectionModeller.java:118)
位于org.glassfish.jersey.server.model.IntrospectionModeller.access$000(IntrospectionModeller.java:80)
位于org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:111)
位于org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:108)
位于org.glassfish.jersey.internal.Errors.process(Errors.java:315)
位于org.glassfish.jersey.internal.Errors.process(Errors.java:297)
位于org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
位于org.glassfish.jersey.server.model.IntrospectionModeller.createResourceBuilder(IntrospectionModeller.java:108)
位于org.glassfish.jersey.server.model.Resource.from(Resource.java:744)
位于org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:400)
位于org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:163)
位于org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:323)
位于org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
位于org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
位于org.glassfish.jersey.internal.Errors.process(Errors.java:315)
位于org.glassfish.jersey.internal.Errors.process(Errors.java:297)
位于org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
位于org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:320)
位于org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:285)
位于org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.(GrizzlyHttpContainer.java:331)
位于org.glassfish.jersey.grizzly2.httpserver.grizzlyhttpserver.createHttpServer(grizzlyhttpserver.java:116)
位于com.getbro.api.Main.startServer(Main.java:32)
位于com.getbro.api.Main.Main(Main.java:42)
... 还有6个
原因:java.lang.ClassNotFoundException:javax.servlet.http.HttpServletRequest
在java.net.URLClassLoader$1.run(URLClassLoader.java:366)
在java.net.URLClassLoader$1.run(URLClassLoader.java:355)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.net.URLClassLoader.findClass(URLClassLoader.java:354)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:425)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 36多
我觉得这很奇怪,因为我根据maven导入了这个类:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
javax.servlet
servlet api
2.5
假如
。。。因此,它应该像预期的那样工作,但事实并非如此 您可以在提供的范围内导入它。那样的话,你只要保证别担心,它会在那里,不会给战争增加任何东西。使用scope
compile
实际导入依赖项 您的依赖项提供了一个范围,这意味着工件将可用于编译,但在运行时不可用。有关此主题的详细信息,请访问或
这是正确的做法,因为ServletAPI不是您想要在WAR、EAR或您正在构建的任何包中拥有的东西。相反,这个servlet api JAR将由您的应用服务器(Tomcat、Jetty…)提供
如果您正在从Eclipse运行web服务项目,则需要告诉他必须使用哪个服务器运行时。(如果您使用的是Eclipse以外的IDE,则必须执行类似的操作):
- 窗口>首选项>服务器>运行时环境
- 如果已经为您的服务器定义了运行时环境(如Tomcat 6.x,如果您正在使用),请取消此窗口。否则,请使用Add。。。按钮
一旦定义了运行时环境:
- 右键单击项目>生成路径>配置生成路径
- “库”选项卡>添加库
- 选择适当的服务器运行时并完成
在IDE中进行测试时,将作用域更改为编译将起作用。但是,当您实际将完成的项目部署到应用程序服务器中时,这将导致问题,因为将有两个servlet api JAR:一个由服务器提供,另一个在WAR中。例如,Tomcat将无法启动应用程序的上下文。事实上,它不会导致任何问题,因为它是一个只包含API的jar。它没有实现类,应用程序将使用co