问题
I'm using the latest TomCat version and I've recently installed Eclipse Java EE version so it's also the latest version. Btw I do have the latest version of JAVA SE (JDK).
This is my class:
package jsp.demo;
public class ClassForJSP {
public static String lowerCase(String data) {
return data.toLowerCase();
}
}
So this is my really simple jsp code:
<%@page import="jsp.demo.ClassForJSP"%>
<html>
<body>
Let's make THIS STATEMENT lower case: <%= ClassForJSP.lowerCase("THIS STATEMENT") %>
</body>
</html>
I'm getting this error:
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [14] in the generated java file: [D:\Java Projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\jspdemo\org\apache\jsp\callJavaClass_jsp.java] Only a type can be imported. jsp.demo.ClassForJSP resolves to a package
An error occurred at line: [4] in the jsp file: [/callJavaClass.jsp] ClassForJSP cannot be resolved 1: <%@page import="jsp.demo.ClassForJSP"%> 2: 3: 4: Let's make THIS STATEMENT lower case: <%= ClassForJSP.lowerCase("THIS STATEMENT") %> 5: 6:
Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:473) org.apache.jasper.compiler.Compiler.compile(Compiler.java:392) org.apache.jasper.compiler.Compiler.compile(Compiler.java:362) org.apache.jasper.compiler.Compiler.compile(Compiler.java:346) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.14
回答1:
This message is telling:
Only a type can be imported. jsp.demo.ClassForJSP resolves to a package
The source code of your Java class indicates that the package is jsp.demo
. But at runtime, the JSP compiler has found a package (i.e. a directory on the classpath) whose name is jsp.demo.ClassForJSP
.
Clearly, something is wrong with the classpath ... and the JSP compiler has raised a red flag.
How come?
- Maybe you are using a "stale" JAR file in the webapp deployed to the server?
- Maybe you have missed a build or deployment step?
- Maybe you have manually assembled the JAR file, or the WAR file that contains it, and have gotten the structure wrong?
- Maybe you are doing this "live" on the Tomcat server, and you have copied a ".class" file to the wrong place?
- Possibly something else?
You will need to look at all of these possibilities yourself.
来源:https://stackoverflow.com/questions/54338873/unable-to-compile-class-for-jsptomcat-9