(I've read other questions on this topic, but none of them have helped)
Okay, I'm trying to import a Java class into my JSP file, but Tomcat gives me the error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [16] in the generated java file: [I:\path\to\index_jsp.java] Only a type can be imported. org.runas.XKCDUtils resolves to a package An error occurred at line: 7 in the jsp file: /index.jsp XKCDUtils cannot be resolved
4: String data = null;
5: int finalComic = 1;
6: int comicNo = 1;
7: finalComic = XKCDUtils.getFinalComic();
8:
9: if (request.getParameter("page") != null)
10: comicNo = Integer.parseInt(request.getParameter("page"));
The import statement in my JSP file is:
<%@page import="java.io.*, java.net.*, org.runas.XKCDUtils" %>
The relevant section of my directory structure is:
webapps
`-ROOT
`-index.jsp
|-WEB-INF
`-web.xml
|-classes
`-org
`-runas
`-XKCDUtils.class
I'm confused because org.runas.XKCDUtils resolves to a package
, and yet it is clearly a class file, in what I believe is the correct directory.
Help, please!
(And don't chastise me for using scriptlets, I'm trying to clean them up!)
This means that your project isn't setup to include the JUnit libraries when it compiles; JUnit is not included in the Java runtime libraries (JRE System Library) so you have to add it to the build path.
To import all the types contained in a particular package, use the import statement with the asterisk ( * ) wildcard character. Now you can refer to any class or interface in the package by its simple name. Note: Another, less common form of import allows you to import the public nested classes of an enclosing class.
It's mandatory that class files reside on the same package or directory as declared in their source file using package keyword, failure to do will result in java.
The classes belonging to the package java. awt are stored in directory " $BASE_DIR\java\awt\ " while the classes of package java.
Okay, this is weird. I split the import into three imports,
<%@page import="java.io.*" %>
<%@page import="java.net.*" %>
<%@page import="org.runas.XKCDUtils" %>
and it magically decided to work. Then when I changed the imports back,
<%@page import="java.io.*, java.net.*, org.runas.XKCDUtils" %>
it magically decided to keep working.
Maybe it had something to do with restarting Tomcat? Well, it works now, anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With