The declared package test does not match the expec

2019-09-07 15:34发布

问题:

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.