The declared package test does not match the expected package

烈酒焚心 提交于 2020-01-06 19:41:31

问题


So, I have this code:

package test;
import test.Pi;
public class Demo {
    public static int pi = 3;   
    public static void main (String args[]) {
        System.out.println("Hello!");
        Pi.main(args);
        System.out.println("Hello again!");
    }
}

But eclipse keeps throwing up an error at the very first line, saying "The declared package test does not match the expected package". Any help apreciated! Thanks!


回答1:


That's not a matter of importing - that means you're trying to declare that the package for this class (Demo) is test, but the compiler error shows that you've got it in the wrong place - you've got it in the root of your source path, instead of in a directory called test under the source root.

Three possible changes:

  • Don't put it in the test package; given the title of your question, it's not clear whether you were trying to do that or not. You don't need to import any classes which are in the same package as the class you're declaring.

  • Move Demo.java into a test folder if it's not already.

  • If Demo.java is already in a test folder, change your build configuration so that its parent directory is the source root.




回答2:


- The error which you have received says that you are using the wrong package name.

- Please see, are you in the test directory or not.

- And please remove the ` before your package name.



来源:https://stackoverflow.com/questions/12823710/the-declared-package-test-does-not-match-the-expected-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!