0

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!

0

2 Answers 2

4

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.

7
  • I should probably clarify-- I'm trying to reference a class in a different package by using the import function.
    – Cowthulhu
    Oct 10, 2012 at 16:08
  • @user1470896: but you're not in a different package, the package declaration says 'package test;' Oct 10, 2012 at 16:10
  • @user1470896: But it's not the import which is causing the error - as you said yourself, the error is on the very first line, which is a package declaration.
    – Jon Skeet
    Oct 10, 2012 at 16:11
  • Sorry if I'm being a bit thick, but I'm trying to access it from a different package. So, for instance, package1/Demo.java is trying to access test/Pi.java
    – Cowthulhu
    Oct 10, 2012 at 16:18
  • 1
    @Cowthulhu: Then I suggest you ask a new question giving details about what's happening... including what your Pi class looks like.
    – Jon Skeet
    Oct 10, 2012 at 16:24
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.