Program Tip

선언 된 패키지가 예상 패키지 ""와 일치하지 않습니다.

programtip 2020. 10. 18. 19:03
반응형

선언 된 패키지가 예상 패키지 ""와 일치하지 않습니다.


Eclipse를 사용하고 있으며 Java를 한동안 사용하지 않았습니다. 그러나 명령 줄에서 코드를 잘 컴파일하고 필요한 .class파일을 생성 할 수 있습니다. Eclipse에서는 The declared package "Devices" does not match the expected package "". 이것은 무엇을 의미하며 어떻게 고칠 수 있습니까?

샘플 코드 :

package Devices;

public final class DevFrequency 
{
    public short messageID;
    public double frequency;
    public short converterID;
    public DevFrequency() 
    {
    }
    public DevFrequency(short _messageID,double _frequency,short _converterID)
    {
        messageID = _messageID;
        frequency = _frequency;
        converterID = _converterID;
    }
}

내 프로젝트의 이름은입니다 DeviceDDS.


Eclipse는 선언 된 패키지가 디렉토리 계층과 일치 할 것으로 예상하므로 Java 파일이 소스 루트 아래의 "Devices"라는 디렉토리에있을 것으로 예상합니다. 현재 파일이 소스 루트에 직접있는 것처럼 보입니다. 따라서 적절한 디렉토리를 만들고 거기로 파일을 이동하십시오.

일반적으로 패키지는 소문자이며 DNS 역순으로 조직 이름을 포함합니다.

com.foo.devices;

파일을 닫았다가 다시 열어보십시오.

파일 위치 또는 패키지 선언에 전혀 문제가없는 경우 이클립스에서이 오류가 발생할 수 있습니다. 이러한 다른 솔루션을 시도하는 데 많은 시간을 보내기 전에 시도해보십시오. 때때로 일식은 혼란스러워집니다. 여러 번 저에게 효과적이었습니다. 나는 이 아이디어Joshua Goldberg 에게 돌렸다 .


해결책 1 : 이클립스 CVS에서 체크 아웃 한 프로젝트에 대해 " 선언 된 패키지가 예상 된 패키지와 일치하지 않습니다 " 라는 오류가 발생했을 때 저에게 도움이 된 한 가지 해결책이 있습니다 .

탐색 모음에서 프로젝트를 클릭하고 '삭제'를 클릭 1.Right
을 2.Make 확인 ' 디스크 삭제 프로젝트 내용을 옵션은' NOT 확인하고 확인을 클릭합니다.
3. 이제 프로젝트가 삭제 된 후 File->
Import- > General- > Existing Projects into Workspace 4. 디렉토리 목록에서 작업 공간을 선택하고 프로젝트 이름 옆에있는 확인란을 선택합니다. ' 마침 '을 클릭하십시오.

해결 방법 2 : 다시 한 번 다음 메시지와 함께이 오류가 발생했습니다.

Eclipse 빌드 오류-java.lang.Object를 해결할 수 없습니다. 여기서 다른 경로 언급을 따라야했고 오류가 사라졌습니다.

그 동안 해결 방법은 프로젝트에서 JRE 시스템 라이브러리를 제거한 다음 다시 추가하는 것입니다. 단계는 다음과 같습니다.

  1. 빌드 오류가있는 프로젝트의 속성으로 이동합니다 (오른쪽 클릭> 속성) "빌드 경로"섹션에서 "라이브러리"탭을 봅니다. 목록에서 "JRE 시스템 라이브러리"를 찾습니다 (누락 된 경우이 오류 메시지는 이클립스 버그이지만 잘못 구성된 프로젝트)
  2. "JRE 시스템 라이브러리"제거
  3. "Add Library ..."를 누르고 "JRE System Library"를 선택하고 프로젝트에 적합한 JRE를 추가합니다 (예 : 'Workspace default JRE').
  4. 라이브러리 선택에서 "마침"을 누르고 프로젝트 속성에서 "확인"을 누른 다음 프로젝트가 다시 빌드 될 때까지 기다립니다.

오류가 해결되기를 바랍니다.


이 문제가 발생했습니다. 패키지 내의 다른 클래스는 괜찮 았지만 한 클래스에는 이에 대한 오류가있었습니다. 패키지 선언에는 문제가 없습니다.

나는 그것을 수정하고 refactor->move일시적으로 다른 패키지로 클래스를 옮긴 다음 refactor->move원래 패키지 돌아 왔습니다.


다음 단계에 따라 문제를 해결했습니다.

  1. 프로젝트 선택-마우스 오른쪽 버튼 클릭-자바 빌드 경로.

  2. 소스 탭에서- srcsrc/main/java.

  3. Eclipse는 모든 프로젝트를 재정렬합니다.


You need to have the class inside a folder Devices.


Create a new package under your project called "Devices" and place your class in it. This is equivalent to the class being placed in a directory called "Devices" in your project source folder.


Make sure that Devices is defined as a source folder in the project properties.


Got the same kind of error but my package was absolutely correct. When I just closed and opened my editor, the error disappears. Hope this might help in some scenarios.


I fixed this by removing an "excluding" attribute for that package in my .classpath file. Remove the attribute, not the whole tag, or "src/java" will cease to be a source folder.

<classpathentry excluding="com/myproject/mypackage/mysubpackage/" kind="src" path="src/java"/>

Make sure that You have created a correct package.You might get a chance to create folder instead of package


  1. Create directory [your.project.name] in workspace root directory of your project.

  2. Copy *.java from "src" to that directory.

  3. Close and reopen project.


This happened to me when I was checking out a project from an svn repository in eclipse. There were jar files in my .m2 folder that eclipse wasn't looking at. To fix the issue I did:

right click project folder Configure > Convert to Maven Project

and that solved the issue.


There are a million answers, but here's another one: copy the files into a new package, delete the old package and rename the new package to the old package's name.


Make sure you are not using default package. Create a new package with name 'devices' and copy this code inside it and use.


I had have this sort situations when I copied classes from other packages/projects.

Menu->Project->Clean usually helps.


In my case I selected the error marker in the Problems tab and deleted it since the Java's file main method was being executed correctly. That is some glitch in Eclipse Neon with the multitude of plugins it has installed.


I faced this issue too when I had imported an existing project to eclipse. It was a gradle project, but while importing I imported it as regular project by clicking General-> Existing Projects into Workspace. To resolve the issue I've added Gradle nature to the project by :::: Right click on Project folder -> Configure-> Add Gradle Nature

참고URL : https://stackoverflow.com/questions/6997524/the-declared-package-does-not-match-the-expected-package

반응형