[英]java ee: class not found despite valid import?!
I use jersey to create some sort of RESTful API. 我使用jersey创建某种RESTful API。 To get information about the current request, I use the HttpServletRequest Object. 为了获得有关当前请求的信息,我使用了HttpServletRequest对象。 I am able to compile the project without problems or errors, but when I run it, I get the following error: 我能够编译项目而不会出现问题或错误,但是当我运行它时,出现以下错误:
[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
I think it's strange, because I import this class per maven: 我认为这很奇怪,因为我每个Maven都会导入此类:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
... so it should work like expected, but it does not! ...因此它应该像预期的那样工作,但事实并非如此!
You import it in provided scope. 您可以在提供的范围内导入它。 That way you just promise don't worry, it'll be there , and nothing is added to war. 这样,您只要保证不用担心,它就会在那里 ,并且战争不会增加任何东西。 Use scope compile
to actually import dependancies. 使用范围compile
来实际导入依赖关系。
Your dependency has a provided
scope, which means that the artifact will be available for compiling, but not during runtime. 您的依赖项具有provided
范围,这意味着该工件可用于编译,但在运行时不可用。 More info on this topic in the Maven documentation or this StackOverflow question . 有关此主题的更多信息,请参见Maven文档或StackOverflow问题 。
That's the right thing to do, because the Servlet API is not something you want to have within your WAR, EAR, or whatever package you're building. 这是正确的做法,因为Servlet API不是您希望在WAR,EAR或正在构建的任何程序包中拥有的东西。 Instead, this servlet-api JAR will be provided by your application server (Tomcat, Jetty...). 相反,此servlet-api JAR将由您的应用程序服务器(Tomcat,Jetty ...)提供。
If you're running your web service project from Eclipse, you need to tell him which server runtime it has to use. 如果要从Eclipse运行Web服务项目,则需要告诉他必须使用哪个服务器运行时。 (If you're using an IDE other than Eclipse, you'll have to do something similar): (如果您使用的是Eclipse以外的IDE,则必须执行类似的操作):
Once there is a runtime environment defined: 一旦定义了运行时环境:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.