声明的包测试与预期的包不匹配 [英] The declared package test does not match the expected package

查看:357
本文介绍了声明的包测试与预期的包不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有以下代码:

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!");
    }
}

但是eclipse总是在第一行抛出一个错误,说声明的包测试与预期的包不匹配". 任何帮助赞赏!谢谢!

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!

推荐答案

这与 import 无关-这意味着您要尝试声明该软件包此类(Demo)是test,但是编译器错误表明您将其放置在错误的位置-您将其放置在源路径的根目录中,而不是在名为test的目录中在源根目录下.

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.

三个可能的更改:

  • 不要将其放在test包中;给定您的问题的标题,还不清楚您是否尝试这样做.您不需要导入与要声明的类在同一包中的任何类.

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

Demo.java移到test文件夹中.

如果test文件夹中已经存在Demo.java ,请更改构建配置,使其 parent 目录为源根目录.

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

这篇关于声明的包测试与预期的包不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆