JSP 错误:只能导入类型。 xyZ 解析为一个包
我收到错误:
org.apache.jasper.JasperException:无法编译 JSP 类:
生成的java文件中第7行发生错误 只能导入一个类型。 org.eresearch.knowledgeportal.model.Category 解析为包
有人已经解释了原因 这里 但我不确定我应该做什么来解决这个问题。仅供参考:我正在使用 Eclipse。我在下面添加了执行导入的代码。 java.util.* 导入工作正常。
<%@ 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();
//
%>
I get the error:
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
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();
//
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
好吧,您并没有真正在您的 web 应用程序上提供足够的详细信息,但我的猜测是您有一个包含类似内容的 JSP:
并且
xyZ
无法在类路径上找到(即不存在于 < code>WEB-INF/classes 也不在WEB-INF/lib
的 JAR 中)。仔细检查您在 Tomcat 上部署的 WAR 是否具有以下结构:
或者捆绑
xyZclass
的 JAR 是否存在于WEB-INF/lib
下。Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that:
And
x.y.Z
can't be found on the classpath (i.e. is not present underWEB-INF/classes
nor in a JAR ofWEB-INF/lib
).Double check that the WAR you deploy on Tomcat has the following structure:
Or that the JAR that bundles
x.y.Z.class
is present underWEB-INF/lib
.好吧,我刚刚解决了。在上次导入中我添加了一个“;”通过复制其他代码示例。我想这是需要的标准行结尾。
于是就
变成了
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
became
如果类名拼写错误或者类不在类路径中,JSP 处理器会说它“解析为包”而不是说它不存在。今天这让我发疯,因为我一直没有看到我犯的错字。
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.
我在 Netbeans 中遇到此错误。与大多数像这样突然出现的奇怪错误一样,我通过转到项目属性、更改源/二进制格式(无关紧要,只是不同)并进行清理和构建来解决它。
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.
我通过在 tomcat lib 目录中添加 jar 解决了这个问题。
I got it resolved by adding the jars in tomcat lib directory.
如果没有更多细节,这听起来像是类的导入声明中的错误。检查所有导入声明是否导入包中的所有类或单个类:
编辑
好的,编辑后,看起来是一个jsp问题。
编辑2
这是另一个论坛条目,问题似乎有相似之处,受害者通过重新安装eclipse解决了这个问题。我会先尝试一下 - 安装第二个 Eclipse 实例,仅使用最必要的插件,一个新的工作区,将项目导入到该干净的工作区中,并希望获得最好的结果...
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:
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...
我知道回复这篇文章有点太晚了,但由于我没有看到任何明确的答案,所以我无论如何都会这样做...
您可能想查看
中的
在你的 Eclipse 上。MANIFEST.MF
META-INF那么您可能需要添加类文件的路径,例如..
Class-Path: WEB-INF/classes
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
inMETA-INF
on your eclipse.then you might need to add the path of your class files like..
Class-Path: WEB-INF/classes
也遇到了这个异常。
环境:配备 Eclipse 的 Mac,使用服务器视图从 Eclipse 内部运行 Tomcat。
无论出于何种原因,Eclipse 都不会将
classes
文件夹复制到WEB-INF
。手动复制classes
文件夹后,一切正常。不知道,或者是 Eclipse 的 bug 或者我错过了一些东西。
Got this exception as well.
Environment: Mac with Eclipse running Tomcat from inside Eclipse using Servers view.
For any reason Eclipse does not copy
classes
folder toWEB-INF
. Afterclasses
folder was manually copied, everything works fine.Don't know, or it is Eclipse bug or I missed something.
在更改类名称中的字母大小写后,我也遇到了这个奇怪的错误。该文件没有按预期复制到 tomcat 服务器,我必须手动删除它并重新部署。也许是因为我使用不区分大小写的操作系统?
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?
我敢打赌,您有一个名为 org.ivec.eresearch.knowledgeportal.model.category(小
c
)的包,并且正在 Windows 等不区分大小写的文件系统上运行或麦克。当类和包存在时,编译器似乎会感到困惑。您可以重命名类“Category”或包“category”,此错误就会消失。不幸的是,我不确定这是 Tomcat 还是 ECJ 错误。
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.
单独生成 .class 文件并将其粘贴到工作区中的相关包中。
刷新项目。
generate .class file separate and paste it into relevant package into the workspace.
Refresh Project.
当混合(在同一个 jsp 页面中)静态 jsp import:
与动态 jsp import:
时,通常会发生这种情况,并且您的类型已由“静态导入”jsp 导入。当“动态导入”jsp需要使用(然后导入)相同类型时->这会生成异常:“只能导入一种类型...”
This typically happens when mixing (in the same jsp page) static jsp import:
with dynamic jsp import:
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..."
我有类似的问题。在 Eclipse 中,我将我的项目与工作正常的示例项目(由 Maven 原型生成)进行了比较。我发现我的项目在 /.classpath 文件中遗漏了 2 行。我复制了这两行,它解决了问题。似乎即使我在项目首选项中设置了构建路径,但 Eclipse 由于某些原因并未相应更新。
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.
我的贡献:我收到此错误是因为我创建了一个名为 3lp 的包。
但是,根据 java 规范,您不能将包命名为以数字开头。
我将其更改为_3lp,现在可以使用了。
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.
如果您使用 Maven 并将 Java 类打包为 JAR,请确保 JAR 是最新的。当然,仍然假设 JAR 在您的类路径中。
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.
我通过将包含导入类的 jar 文件添加到 WEB-INF/Lib 中解决了这个问题。
I resolved it by adding the jar file containing the imported classes into
WEB-INF/Lib
.您是否尝试像我一样导入一个被覆盖的类?
如果是这样,则您的重写类位于错误的包中,或者根本不存在。
创建类或将类移动到正确的位置(src/[package.package].[class])可以解决您的问题。
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.
对我来说这是一个错误的部署。部署正确,一切正常(检查 我的问题了解详细信息)。
To me it was a wrong deployment. Deployed properly, everything works (check my question for details).
所以我有同样的问题,只是想给出我的解决方案。也许有人面临同样的问题。
我已经正确配置了我的工件,但不知何故 intellij 混淆了某些内容并删除了我的工件之一中的 WEB-INF 文件夹。
当我查看 .war 文件的内容时,每个类都存在于正确的文件夹结构中。所以我并不认为缺少什么。但是,当我检查 Intellij 中的工件并将其与工作工件进行比较时,我意识到包含类和库的 WEB-INF 文件夹不存在。
将它们添加到工件中,它应该可以工作。
TLDR:Intellij 删除了我的 .war 工件中的 WEB-INF 文件夹。只需检查一下您的是否也丢失了。 (项目结构 -> 工件)
So I had the same issue and just wanted to give my solution. Maybe someone faces the same problem.
I had properly configured my artifact but somehow intellij mixed something up and deleted the WEB-INF folder in one of my artifacts.
When I viewed the contents of my .war file every class was present in the correct folder structure. So I didn't think that anything was missing. But when I check the artifacts in Intellij and compared it to a working artifact then I realized, that the WEB-INF folder with the classes and libs were not present.
Add them to the artifact and it should work.
TLDR: Intellij deleted the WEB-INF Folder in my .war artifact. Just check if yours is missing aswell. (Project Strucutre -> Artifacts)
tomcat 文件夹结构中有 Webapps/ROOT/,其中必须存在 WEB-INF 文件夹。如果将 WEB-INF 放在 Webapps/ tomcat 中,则找不到类文件和 jar。
The tomcat folder structure has Webapps/ROOT/ inside where the WEB-INF folder has to be present. If you place your WEB-INF inside Webapps/ tomcat is not locating the class files and jars.
只是添加另一种方法来实现这个奇怪的错误
对我来说,发生的事情是通过使用 TOMCAT_HOME 变量,
classpath
从 tomcat 获得了一些 jar。我没有设置该变量,并且在当前工作的一年多时间里从未需要过它。但是这个特定项目期望在
classpath
中包含此变量,而 Eclipse 不会告诉我发生了classpath
错误。Just adding yet another way of achieving this weird error
To me what happened is that the
classpath
had a few jars from tomcat by using theTOMCAT_HOME
variable.I didn't have that variable set and never needed it in over a year working in my current job. But this specific project was expecting this variable in the
classpath
and Eclipse wouldn't tell me there was aclasspath
error going on.我遇到了同样的错误,但我的 java 文件和包没有任何问题。后来我注意到文件夹名称WEB-INF是这样写的“WEB_INF”。因此,仅更正文件夹名称即可解决问题
I was getting the same error and nothing was wrong with my java files and packages. Later I noticed that the folder name WEB-INF was written like this "WEB_INF". So correcting just the folder name solved the issue
从 Eclipse 运行时我遇到了同样的问题。为了解决这个问题,我进入配置构建路径并从构建路径中删除了 src 文件夹。然后关闭并重新打开该项目。然后我将 src 文件夹添加到构建路径中。 src 文件夹包含我试图访问的 java 类。
I had this same issue when running from Eclipse. To fix the issue I went into the Configure Build Path and removed the src folder from the build path. Then closed and reopened the project. Then I added the src folder to the build path. The src folder contained the java class I was trying to access.
这在 Tomcat 9.0.62 上困扰着我们。事实证明我们的问题就像权限一样简单。一个类正在工作,但另一个类没有(由于权限):
/WEB-INF/classes/ 列表:
/WEB-INF/classes/ 目录中的一个简单的“
chmod 755 non_working_class_directory
”命令解决了这个问题。我希望这对某人有帮助。
This plagued us on Tomcat 9.0.62. It turns out our issue was as simple as permissions. One class was working, but another wasn't (due to permissions):
/WEB-INF/classes/ listing:
A simple "
chmod 755 non_working_class_directory
" command in the /WEB-INF/classes/ directory fixed the issue.I hope that helps someone.
您必须从包中导入某些内容,例如类、枚举或接口,如下所示:
或者,从包中导入所有内容(不推荐)
编辑:也许我读得不够仔细。您尝试从文件系统上导入的包在哪里?是在WEB-INF/lib下吗?
You have to import something FROM the package, like a class, enum, or interfacee, like this:
or, import everything from the package (not recommended)
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?
还有更多细节吗? (这是在链接网页中的 JSP 中吗?)
如果是这样,您始终可以使用完全限定的类名。
而不是:
你可以使用
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:
you can use
我有同样的错误消息,我的处理方法如下:
I had the same error message and my way to deal with it is as follows: