Java 错误:只能导入一个类型。XYZ 解析为包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1858463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java error: Only a type can be imported. XYZ resolves to a package
提问by Ankur
I get the error: "Only a type can be imported. XYZ resolves to a package."
我收到错误消息:“只能导入一个类型。XYZ 解析为一个包。”
Someone has explained the cause herebut 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.
有人在这里解释了原因,但我不确定我应该怎么做才能解决这个问题。仅供参考:我正在使用 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();
//
%>
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
采纳答案by Ankur
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;" %>
回答by Ankur
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?
编辑:也许我读得不够仔细。您尝试从中导入的包位于文件系统上的什么位置?是在 WEB-INF/lib 下吗?
回答by William Billingsley
Are there more details? (Is this in a JSP as in the linked webpage?)
有更多细节吗?(这在 JSP 中是否与链接网页中的一样?)
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;
回答by Andreas Dolk
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.
OK,编辑完后,貌似是jsp的问题。
Edit 2
编辑 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 解决了它。我会先尝试那个 - 安装第二个 eclipse 实例,只安装最必要的插件,一个新的工作区,项目导入到那个干净的工作区,并希望最好......
回答by Pascal Thivent
Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that:
好吧,您并没有真正提供有关您的 web 应用程序的足够详细信息,但我的猜测是您有一个类似这样的 JSP:
<%@ 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
).
并且x.y.Z
在类路径上找不到(即不存在WEB-INF/classes
于 的 JAR下也不存在于WEB-INF/lib
)。
Double check that the WAR you deploy on Tomcat has the following structure:
仔细检查您在 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
Or that the JAR that bundles x.y.Z.class
is present under WEB-INF/lib
.
或者捆绑的 JARx.y.Z.class
存在于WEB-INF/lib
.
回答by optimystery
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.
你可能想检查出MANIFEST.MF
在META-INF
你的日食。
then you might need to add the path of your class files like..
那么您可能需要添加类文件的路径,例如..
Class-Path: WEB-INF/classes
Class-Path: WEB-INF/classes
回答by jediz
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?
在更改类名中的字母大小写后,我也遇到了这个奇怪的错误。该文件没有按预期复制到 tomcat 服务器,我不得不手动删除它并重新部署。也许是因为我使用不区分大小写的操作系统?
回答by Tharaka
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.
我有一个类似的问题。在 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>
回答by Cocu_1012
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.
我的贡献:我收到此错误是因为我创建了一个名为 3lp 的包。但是,根据 java 规范,您不能以数字开头命名您的包。我将其更改为 _3lp,现在可以使用了。
回答by Gullbyrd
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.
我在 Netbeans 中遇到了这个错误。与大多数像这样突然出现的奇怪错误一样,我通过转到项目属性、更改源/二进制格式(无关紧要,只是一些不同的内容)并进行清理和构建来解决它。