1

I have build two classes named PackageTest.java (in the desktop dir) and Employee.java (in the desktop/com/wenhu/corejava dir).

In the Employee.java file, I wrote in the first line:

package com.wenhu.corejava;

Then in the PackageTest.java file, I wrote in the first line:

import com.wenhu.corejava.*;

However, the compiler complains:

    PackageTest.java:8: error: cannot access Employee
        Employee harry = new Employee("Harry", 50000, 1989, 10, 1);
        ^
  bad class file: .\Employee.class
    class file contains wrong class: com.wenhu.corejava.Employee
    Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error

Interestingly, if I wrote:

import com.wenhu.corejava.Employee;

The compiler is OK! Could anyone tell me why this is happened? I though the wildcard * could represent the Employee Class...

Thanks a lot!

Oliver
  • 147
  • 9
  • 2
    _"class file contains wrong class: com.wenhu.corejava.Employee Please remove or make sure it appears in the correct subdirectory of the classpath."_ You messed up your project structure. make sure that `Employee.java` is in the `com/wenhu/corejava` directory – litelite Aug 14 '17 at 12:43

2 Answers2

2

Simple:

bad class file: .\Employee.class

class file contains wrong class: com.wenhu.corejava.Employee

Your setup is somehow messed up. It seems that you have a class file for Employee in your current directory.

The package name within the class file must match the file system location!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Haaah, I feel like drinking a big beer, I found that rat in the current directory, thank you so much for answering my noob question... It helps a lot!!! – Oliver Aug 14 '17 at 12:52
  • Sure. And for the record: please dont forget about about accepting one of the answers. – GhostCat Aug 14 '17 at 12:56
  • oh, still have a question. Why it turns to be right after I import the whole path including the Employee, like import com.wenhu.corejava.Employee; why the compiler doesn't have the name confusion this time, even I still have the rat in the current directory? – Oliver Aug 14 '17 at 13:04
  • I have to admit: I can't say. We would have understand all aspects of your setup completely. Like class path setup, folder structure, ... – GhostCat Aug 14 '17 at 13:08
  • 3
    The case `import com.wenhu.corejava.Employee` works because this is a "single-type-import" that shadows the `Employee` class of the current package. A wildcard import (type-import-on-demand) does not shadow the class `Employee` of the current package. See the JLS rules about shadowing: https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.3 – Thomas Kläger Aug 14 '17 at 13:29
  • Thanks a lot! Now I get it! – Oliver Aug 14 '17 at 13:50
2
package com.wenhu.corejava;

This statement in the Employee class means that your class file Employee.java must be located in the directory com/wenhu/corejava/. But in your case it is in the directory which your java compiler as root of all sources understand, i.e. the default package.

To resolve your problem, either remove the package declaration mentioned above, which is not recommended, or create the corresponding directory and move the source file Employee.java to it.

A4L
  • 17,353
  • 6
  • 49
  • 70