可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I get the error: "Only a type can be imported. XYZ resolves to a package."
Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The java.util.* import works fine.
<%@ page import="java.util.*"%>
<%@ page import="org.eresearch.knowledgeportal.model.Category"%>
<%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao"%>
<%
CategoryDao catDao = new CategoryDao();
ArrayList<Category> catList = catDao.selectCategory();
//
%>
Edit: the actual error is below:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 7 in the generated java file
Only a type can be imported. org.eresearch.knowledgeportal.model.Category resolves to a package
回答1:
Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that:
<%@ page import="java.util.*,x.y.Z"%>
And x.y.Z
can't be found on the classpath (i.e. is not present under WEB-INF/classes
nor in a JAR of WEB-INF/lib
).
Double check that the WAR you deploy on Tomcat has the following structure:
my-webapp
|-- META-INF
| `-- MANIFEST.MF
|-- WEB-INF
| |-- classes
| | |-- x
| | | `-- y
| | | `-- Z.class
| | `-- another
| | `-- packagename
| | `-- AnotherClass.class
| |-- lib
| | |-- ajar.jar
| | |-- bjar.jar
| | `-- zjar.jar
| `-- web.xml
|-- a.jsp
|-- b.jsp
`-- index.jsp
Or that the JAR that bundles x.y.Z.class
is present under WEB-INF/lib
.
回答2:
OK I just solved it. In the last import I added a ";" by copying other code examples. I guess it's the standard line ending that is required.
So
<%@ page import="java.util.*" %>
<%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
<%@ page import="org.eresearch.knowledgeportal.model.Category" %>
became
<%@ page import="java.util.*" %>
<%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
<%@ page import="org.eresearch.knowledgeportal.model.Category;" %>
回答3:
If you spell the class name wrong or the class isn't on the classpath, the JSP processor will say it "resolves to a package" rather than that it doesn't exist. This was driving me crazy today as I kept not seeing a typo I'd made.
回答4:
Without further details, it sounds like an error in the import declaration of a class. Check, if all import declarations either import all classes from a package or a single class:
import all.classes.from.package.*;
import only.one.type.named.MyClass;
Edit
OK, after the edit, looks like it's a jsp problem.
Edit 2
Here is another forum entry, the problem seems to have similarities and the victim solved it by reinstalling eclipse. I'd try that one first - installing a second instance of eclipse with only the most necessary plugins, a new workspace, the project imported into that clean workspace, and hope for the best...
回答5:
I got this error in Netbeans. As with most bizarre errors like this that appear out of nowhere, I resolve it by going to project properties, changing the Source/Binary Format (doesn't matter to what, just something different), and doing a clean and build.
回答6:
I got it resolved by adding the jars in tomcat lib directory.
回答7:
I experienced this weird error too, after changing letter case in the name of a class. The file was not copied to a tomcat server as expected, I had to delete it manually and redeploy. Maybe because I use case insensitive operating system?
回答8:
I know it's kinda too late to reply to this post but since I don't see any clear answer i'd do it anyway...
you might wanna check out the MANIFEST.MF
in META-INF
on your eclipse.
then you might need to add the path of your class files like..
Class-Path: WEB-INF/classes
回答9:
generate .class file separate and paste it into relevant package into the workspace.
Refresh Project.
回答10:
To me it was a wrong deployment. Deployed properly, everything works (check my question for details).
回答11:
You have to import something FROM the package, like a class, enum, or interfacee, like this:
import some.package.SomeClass;
or, import everything from the package (not recommended)
import some.package.*;
edit: maybe I didn't read close enough. Where is the package you're trying to import from located on the filesystem? Is it under WEB-INF/lib?
回答12:
Are there more details? (Is this in a JSP as in the linked webpage?)
If so, you can always just use the fully qualified class name.
rather than:
import foo.bar.*;
Baz myBaz;
you can use
foo.bar.Baz myBaz;
回答13:
I had a similar issue. In eclipse I compared my project with a sample project which is working fine (generated by a maven archetype). I found my project has missed 2 lines in /.classpath file. I copied those 2 lines and it fixed the issue. It seems even though I set build path in project preferences, eclipse has not updated accordingly for some reasons.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
...
</classpath>
回答14:
My contribution: I got this error because I created a package named 3lp.
However, according to java spec, you are not allowed to name your package starts with a number.
I changed it to _3lp, now it works.
回答15:
My bet is that you have a package called org.ivec.eresearch.knowledgeportal.model.category (small c
) and are running on a non-case sensitive filesystem like Windows or Mac. It seems that the compiler gets confused when a class and package exist.
You can either renamed the class "Category" or the package "category" and this error will go away. Unfortunately I'm not sure if this is a Tomcat or ECJ bug.
回答16:
If you are using Maven and packaging your Java classes as JAR, then make sure that JAR is up to date. Still assuming that JAR is in your classpath of course.
回答17:
I resolved it by adding the jar file containing the imported classes into WEB-INF/Lib
.
回答18:
Are you attempting to import an overridden class like I was?
If so, your overridden class is in the wrong package, or simply non-existent.
Creating, or moving the class into the correct location (src/[package.package].[class]) could solve your problem.
回答19:
This typically happens when mixing (in the same jsp page) static jsp import:
<%@include file="...
with dynamic jsp import:
<jsp:include page="...
and your type has been already imported by the "static imported" jsp. When the same type needs to be used (and then imported) by the "dynamically imported" jsp -> this generate the Exception: "Only a type can be imported..."
回答20:
I had the same error message and my way to deal with it is as follows:
- First go check the file directory where your Tomcat is publishing your web application, e.g. D:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\myDatatable\WEB-INF\classes, in which we normally put our classes. If this is not the place where you put your classes, then you have to find out where by default it is by right click your web application root name-->Build Path-->Configure Build Bath...-->Then check the "Source" Tab and find out the field value of "Default output folder". This shall be the place where Tomcat put your classes.
- You would see that XYZ class is not yet built. In order to build it, you could go to Menu "Project"-->"Clean..."-->Select your web application to clean.
- After it's completed, try restart your tomcat server and go check the file directory again. Your class should be there. At least it works for me. Hope it helps.