简体   繁体   English

声明的软件包测试与预期的软件包不匹配

[英]The declared package test does not match the expected package

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". 但是eclipse一直在第一行抛出错误,说“声明的包测试与预期的包不匹配”。 Any help apreciated! 任何帮助赞赏! Thanks! 谢谢!

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. 这不是导入的问题 -这意味着您试图声明该类( Demo )的包为test ,但是编译器错误表明您将其放置在错误的位置-您已将其放置在源路径的根目录,而不是源根目录下名为test的目录中。

Three possible changes: 三种可能的更改:

  • Don't put it in the test package; 不要将其放在test包中; 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. 如果尚未将Demo.java移到test文件夹中,则将其移动。

  • If Demo.java is already in a test folder, change your build configuration so that its parent directory is the source root. 如果Demo.java 已经处于test文件夹,更改您的构建配置,这样它的目录是源根。

- 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. -并且请在包裹名称前删除`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM