Java 无法为 JSP 加载类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4501829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to load class for JSP
提问by theJava
Exception stack trace
异常堆栈跟踪
org.apache.jasper.JasperException: Unable to load class for JSP
org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:599)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:143)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:321)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
java.lang.ClassNotFoundException: org.apache.jsp.redirect_jsp
java.net.URLClassLoader.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(Unknown Source)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:131)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:597)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:143)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:321)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
My redirect.jsp file contents
我的 redirect.jsp 文件内容
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("user/list.htm"); %>
采纳答案by BalusC
Under the covers of the servletcontainer, JSP's are compiled to Java classes before they get executed.
在 servletcontainer 的掩护下,JSP 在执行之前被编译为 Java 类。
The exception
例外
java.lang.ClassNotFoundException: org.apache.jsp.redirect_jsp
means that the redirect.jsp
file in the root of your webcontent folder failed to compile which in turn often means that it contains some raw Java code in scriptlets<% %>
which contains syntax errors. You need to fix those syntax errors so that the servletcontainer can compile those JSP files. The general concensus is however that scriptletsare a poor practice. You should consider if that Java code doesn't better belong in a fullworthy Java class, controlled by a Servlet
or a Filter
.
意味着redirect.jsp
webcontent 文件夹根目录中的文件无法编译,这通常意味着它在包含语法错误的scriptlet 中包含一些原始 Java 代码<% %>
。您需要修复这些语法错误,以便 servletcontainer 可以编译这些 JSP 文件。然而,普遍的共识是scriptlet是一种糟糕的做法。您应该考虑该 Java 代码是否更适合属于由 aServlet
或 a控制的完全有价值的 Java 类Filter
。
Another possible cause is that the work cache of the servletcontainer is messed up. This can happen when developing with a poor IDE plugin. You'd like to clean the work cache. In for example Eclipse, you can do that by rightclick the server and choosing Clean. Otherwise it has to be done manually by deleting everything in work cache of the servletcontainer in question. In case of for example Tomcat, that's then everything in side its /work
folder.
另一个可能的原因是 servletcontainer 的工作缓存混乱。使用糟糕的 IDE 插件进行开发时可能会发生这种情况。您想清理工作缓存。例如,在 Eclipse 中,您可以通过右键单击服务器并选择Clean 来完成此操作。否则,必须通过删除相关 servletcontainer 的工作缓存中的所有内容来手动完成。例如,对于 Tomcat,这就是其/work
文件夹中的所有内容。
回答by Simon Gibbs
I was getting this error because I had a JSP API dependency in my WAR's pom.xml:
我收到此错误是因为我的 WAR 的 pom.xml 中有一个 JSP API 依赖项:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
Changing it to this fixed it:
将其更改为此修复了它:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
One assumes this is due to a duplicate class being present during the compile phase. Without BalusCexplaining it's a compilation issue I'd have never guessed!
人们认为这是由于在编译阶段存在重复的类。没有BalusC解释这是一个编译问题,我从来没有猜到过!
回答by Darshil
I was also facing this problem which is due to library jar files like jetty-util-6.0.0rc0.jar, jasper-compiler-jdt-5.5.23.jar, jasperreports-3.0.0.jar. My answer may not be proper because right now I'm a beginner but at least you can try... Thank u,
我也面临这个问题,这是由于库 jar 文件,如 jetty-util-6.0.0rc0.jar、jasper-compiler-jdt-5.5.23.jar、jasperreports-3.0.0.jar。我的回答可能不正确,因为现在我是初学者,但至少你可以尝试......谢谢你,
回答by Yoku
I had similar problem on the spring template Spring MVC
hello world example,generated by IntelliJ. The InternalResourceViewResolver would not resolve the Hello_JSP.java file. I had to change it to following dependency
我在Spring MVC
IntelliJ 生成的 spring 模板hello world 示例上遇到了类似的问题。InternalResourceViewResolver 不会解析 Hello_JSP.java 文件。我不得不将其更改为以下依赖项
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
Hope it can help someone.
希望它可以帮助某人。
回答by user3338098
This can happen without apparent reason when you run out of disk space. Tomcat cant create the class file but goes ahead and inappropriately assumes it was successful, and then complains latter.
当您用完磁盘空间时,这可能会在没有明显原因的情况下发生。Tomcat 无法创建类文件,但继续并错误地假设它已成功,然后抱怨后者。
回答by Maksim Savchuk
Another reason for this exception might be lack of write permission. If tomcat was started on linux machine by a root user it will create work/ directory with owner root. If you will try to start tomcat with special user that has lesser permissions it will fail to compile JSP files because of that. So you might try two solutions:
此异常的另一个原因可能是缺少写入权限。如果 tomcat 是由 root 用户在 linux 机器上启动的,它将创建具有所有者 root 的工作/目录。如果您尝试使用具有较小权限的特殊用户启动 tomcat,它将因此无法编译 JSP 文件。因此,您可以尝试两种解决方案:
- Change ownership of tomcat work folder using
chown tomcat_user -R work/
- Remove work directory before starting tomcat as user with lesser permissions using
rm -R work/
- 使用更改 tomcat 工作文件夹的所有权
chown tomcat_user -R work/
- 使用权限较低的用户启动 tomcat 之前删除工作目录
rm -R work/