development

Java 오류 : 유형 만 가져올 수 있습니다.

big-blog 2020. 10. 24. 11:10
반응형

Java 오류 : 유형 만 가져올 수 있습니다. XYZ는 패키지로 해석됩니다.


"유형 만 가져올 수 있습니다. XYZ가 패키지로 확인됩니다."라는 오류가 표시됩니다.

누군가 여기서 원인을 설명 했지만이 문제를 해결하기 위해 무엇을해야하는지 잘 모르겠습니다. 참고 : 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();

 //
 %>

편집 : 실제 오류는 다음과 같습니다.

 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

글쎄, 당신은 실제로 웹 응용 프로그램에 대한 충분한 세부 정보를 제공하지 않지만 내 생각에는 다음과 같은 JSP가 있습니다.

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

그리고 x.y.Z클래스 경로에서 찾을 수 없습니다 (즉 WEB-INF/classes,의 JAR 아래에 존재하지 않음 WEB-INF/lib).

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

또는 번들 x.y.Z.class로 제공되는 JAR이 WEB-INF/lib.


네, 방금 해결했습니다. 마지막 가져 오기에서 ";"를 추가했습니다. 다른 코드 예제를 복사하여. 나는 그것이 필요한 표준 라인 엔딩이라고 생각합니다.

그래서

<%@ 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;" %>

클래스 이름의 철자를 잘못 입력했거나 클래스가 클래스 경로에 없으면 JSP 프로세서는 존재하지 않는다는 대신 "패키지로 해결됨"이라고 말합니다. 내가 만든 오타를 계속 보지 못했기 때문에 이것은 오늘 나를 미치게 만들었습니다.


Netbeans에서이 오류가 발생했습니다. 갑자기 나타나는 이와 같은 대부분의 기괴한 오류와 마찬가지로 프로젝트 속성으로 이동하여 소스 / 바이너리 형식을 변경하고 (무엇에는 중요하지 않으며, 단지 다른 것임) 정리하고 빌드하여 문제를 해결합니다.


자세한 내용이 없으면 클래스의 가져 오기 선언 오류처럼 들립니다. 모든 가져 오기 선언이 패키지 또는 단일 클래스에서 모든 클래스를 가져 오는지 확인하십시오.

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

편집하다

좋아, 편집 후 jsp 문제인 것 같습니다.

편집 2

여기에 또 다른 포럼 항목 이 있습니다. 문제는 유사점이있는 것으로 보이며 피해자는 이클립스를 다시 설치하여 해결했습니다. 먼저 가장 필요한 플러그인, 새 작업 공간, 깨끗한 작업 공간으로 가져온 프로젝트로 이클립스의 두 번째 인스턴스를 설치하고 최선을 다할 것입니다.


tomcat lib 디렉토리에 항아리를 추가하여 해결했습니다.


수업 이름에서 대소 문자를 변경 한 후에도이 이상한 오류가 발생했습니다. 파일이 예상대로 tomcat 서버에 복사되지 않았으므로 수동으로 삭제하고 다시 배포해야했습니다. 대소 문자를 구분하지 않는 운영 체제를 사용하기 때문일까요?


이 게시물에 답하기에는 너무 늦었다는 것을 알고 있지만 명확한 답변이 보이지 않기 때문에 어쨌든 할 것입니다 ...

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


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


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


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?


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;

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>

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.


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.


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.


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


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.


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


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)


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.


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.

참고URL : https://stackoverflow.com/questions/1858463/java-error-only-a-type-can-be-imported-xyz-resolves-to-a-package

반응형