Reputation: 157
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!
Upvotes: 1
Views: 67
Reputation: 17595
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.
Upvotes: 2
Reputation: 140533
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!
Upvotes: 2