JSP 错误:只能导入类型。 xyZ 解析为一个包

发布于 2024-08-13 16:23:18 字数 733 浏览 5 评论 0原文

我收到错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

孤独患者 2024-08-20 16:23:18

好吧,您并没有真正在您的 web 应用程序上提供足够的详细信息,但我的猜测是您有一个包含类似内容的 JSP:

<%@ page import="java.util.*,x.y.Z"%> 

并且 xyZ 无法在类路径上找到(即不存在于 < code>WEB-INF/classes 也不在 WEB-INF/lib 的 JAR 中)。

仔细检查您在 Tomcat 上部署的 WAR 是否具有以下结构:

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

或者捆绑 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:

<%@ 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.

自演自醉 2024-08-20 16:23:18

好吧,我刚刚解决了。在上次导入中我添加了一个“;”通过复制其他代码示例。我想这是需要的标准行结尾。

于是就

<%@ page import="java.util.*" %>
<%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
<%@ page import="org.eresearch.knowledgeportal.model.Category" %>

变成了

 <%@ page import="java.util.*" %>
 <%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
 <%@ page import="org.eresearch.knowledgeportal.model.Category;" %>

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;" %>
伤痕我心 2024-08-20 16:23:18

如果类名拼写错误或者类不在类路径中,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.

秋千易 2024-08-20 16:23:18

我在 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.

慵挽 2024-08-20 16:23:18

我通过在 tomcat lib 目录中添加 jar 解决了这个问题。

I got it resolved by adding the jars in tomcat lib directory.

夜夜流光相皎洁 2024-08-20 16:23:18

如果没有更多细节,这听起来像是类的导入声明中的错误。检查所有导入声明是否导入包中的所有类或单个类:

import all.classes.from.package.*;
import only.one.type.named.MyClass;

编辑

好的,编辑后,看起来是一个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:

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...

岁月无声 2024-08-20 16:23:18

我知道回复这篇文章有点太晚了,但由于我没有看到任何明确的答案,所以我无论如何都会这样做...

您可能想查看 中的 MANIFEST.MF META-INF 在你的 Eclipse 上。

那么您可能需要添加类文件的路径,例如..

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 in META-INF on your eclipse.

then you might need to add the path of your class files like..

Class-Path: WEB-INF/classes

旧竹 2024-08-20 16:23:18

也遇到了这个异常。

环境:配备 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 to WEB-INF. After classes folder was manually copied, everything works fine.

Don't know, or it is Eclipse bug or I missed something.

记忆消瘦 2024-08-20 16:23:18

在更改类名称中的字母大小写后,我也遇到了这个奇怪的错误。该文件没有按预期复制到 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?

话少心凉 2024-08-20 16:23:18

我敢打赌,您有一个名为 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.

瑾夏年华 2024-08-20 16:23:18

单独生成 .class 文件并将其粘贴到工作区中的相关包中。
刷新项目。

generate .class file separate and paste it into relevant package into the workspace.
Refresh Project.

寄离 2024-08-20 16:23:18

当混合(在同一个 jsp 页面中)静态 jsp import:

<%@include file="...

与动态 jsp import:

<jsp:include page="...

时,通常会发生这种情况,并且您的类型已由“静态导入”jsp 导入。当“动态导入”jsp需要使用(然后导入)相同类型时->这会生成异常:“只能导入一种类型...”

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..."

明月松间行 2024-08-20 16:23:18

我有类似的问题。在 Eclipse 中,我将我的项目与工作正常的示例项目(由 Maven 原型生成)进行了比较。我发现我的项目在 /.classpath 文件中遗漏了 2 行。我复制了这两行,它解决了问题。似乎即使我在项目首选项中设置了构建路径,但 Eclipse 由于某些原因并未相应更新。

<?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>

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>
镜花水月 2024-08-20 16:23:18

我的贡献:我收到此错误是因为我创建了一个名为 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.

栀子花开つ 2024-08-20 16:23:18

如果您使用 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.

随风而去 2024-08-20 16:23:18

我通过将包含导入类的 jar 文件添加到 WEB-INF/Lib 中解决了这个问题。

I resolved it by adding the jar file containing the imported classes into WEB-INF/Lib.

神回复 2024-08-20 16:23:18

您是否尝试像我一样导入一个被覆盖的类?

如果是这样,则您的重写类位于错误的包中,或者根本不存在。

创建类或将类移动到正确的位置(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.

夏尔 2024-08-20 16:23:18

对我来说这是一个错误的部署。部署正确,一切正常(检查 我的问题了解详细信息)。

To me it was a wrong deployment. Deployed properly, everything works (check my question for details).

寂寞陪衬 2024-08-20 16:23:18

所以我有同样的问题,只是想给出我的解决方案。也许有人面临同样的问题。

我已经正确配置了我的工件,但不知何故 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)

肥爪爪 2024-08-20 16:23:18

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.

德意的啸 2024-08-20 16:23:18

只是添加另一种方法来实现这个奇怪的错误

对我来说,发生的事情是通过使用 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 the TOMCAT_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 a classpath error going on.

酒中人 2024-08-20 16:23:18

我遇到了同样的错误,但我的 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

够钟 2024-08-20 16:23:18

从 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.

情绪失控 2024-08-20 16:23:18

这在 Tomcat 9.0.62 上困扰着我们。事实证明我们的问题就像权限一样简单。一个类正在工作,但另一个类没有(由于权限):

/WEB-INF/classes/ 列表:

drwxr-xr-x. 3 tomcat tomcat 4096 Dec  1 08:52 working_class_directory
drw-r--r--. 2 tomcat tomcat 4096 Dec  1 10:14 non_working_class_directory

/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:

drwxr-xr-x. 3 tomcat tomcat 4096 Dec  1 08:52 working_class_directory
drw-r--r--. 2 tomcat tomcat 4096 Dec  1 10:14 non_working_class_directory

A simple "chmod 755 non_working_class_directory" command in the /WEB-INF/classes/ directory fixed the issue.

I hope that helps someone.

长安忆 2024-08-20 16:23:18

您必须从包中导入某些内容,例如类、枚举或接口,如下所示:

import some.package.SomeClass;

或者,从包中导入所有内容(不推荐)

import some.package.*;

编辑:也许我读得不够仔细。您尝试从文件系统上导入的包在哪里?是在WEB-INF/lib下吗?

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?

半透明的墙 2024-08-20 16:23:18

还有更多细节吗? (这是在链接网页中的 JSP 中吗?)

如果是这样,您始终可以使用完全限定的类名。
而不是:

import foo.bar.*;
Baz myBaz;

你可以使用

foo.bar.Baz myBaz;

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;
︶葆Ⅱㄣ 2024-08-20 16:23:18

我有同样的错误消息,我的处理方法如下:

  1. 首先检查 Tomcat 发布 Web 应用程序的文件目录,例如 D:\Java\workspace.metadata.plugins\org.eclipse.wst。 server.core\tmp1\wtpwebapps\myDatatable\WEB-INF\classes,我们通常将类放在其中。如果这不是您放置类的位置,那么您必须通过右键单击您的 Web 应用程序根名称 --> 构建路径 --> 配置构建浴...来找出默认情况下它的位置;然后检查“源”选项卡并找到“默认输出文件夹”字段值。这将是 Tomcat 放置类的地方。
  2. 您会看到 XYZ 类尚未构建。为了构建它,您可以进入菜单“项目”-->“清理...”-->选择要清理的 Web 应用程序。
  3. 完成后,尝试重新启动tomcat服务器并再次检查文件目录。你的班级应该在那里。至少它对我有用。希望有帮助。

I had the same error message and my way to deal with it is as follows:

  1. 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.
  2. 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.
  3. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文