0

I imported a jar package in the external libraries directory.Under the src directory, Test1 can reference classes from HelloJava.jar package and successfully run , but Test2 which in the subdirectory(com.cau) can not.

As the following pictures show:

picture1: picture 1

picture2: picture 2

Test1 code is:

public class Test1 {
    public static void main(String[] args) {
        Puppy MyPuppy = new Puppy("Jerry");
        MyPuppy.setPuppyAge(2);
        MyPuppy.getAge();
    }
}

picture3: picture 3

Test2 code is:

package com.cau;
public class Test2 {
    public static void main(String[] args) {
        Puppy MyPuppy = new Puppy("Jerry");
        MyPuppy.setPuppyAge(2);
        MyPuppy.getAge();
        System.out.println("123456");
    }

}

The class Puppy is in HelloJava.jar package. In the third picture, the red color "Puppy" means Test2 was failed to reference HelloJava.jar.

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • The problem is that your classes in that jar are in the default package, which means they cannot be imported nor used in classes declared in packages. So given `Test1` is also in the default package, it can use `Puppy`, but `Test2` can't because it is in the package `com.au`, and therefor can't use classes from the default package. Except for trivial code, you should avoid using the default package, and you definitely shouldn't use the default package for libraries. – Mark Rotteveel Jul 07 '20 at 18:05
  • Thanks a lot ! I really appreciate your suggestions and private messages.KNOWING WHY , I will move classes in default package to a new package. – liaofanQAQ Jul 07 '20 at 19:08

0 Answers0