compiling java package classes. Cannot access Class. class file contains wrong class while working with packages

I was trying to compile a few files in a package in java. The package name is library. Please have a look at the following details.

This is my Directory Structure:

javalearning
---library
------ParentClass.java
------ChildClass.java

I tried to compile in the following way:

current directory: javalearning

javac library/ParentClass.java   //this compilation works fine 
javac library/ChildClass.java    //error over here

The following is the ParentClass.java:

package library;
class Parentclass{
...
}

The following is the ChildClass.java:

package library;
class ChildClass extends ParentClass{
...
}

The error is as follows:

cannot access ParentClass
bad class file: .\library\ParentClass.class
Please remove or make sure it appears in the correct sub directory of the classpath
Jon Skeet
people
quotationmark

You've got a casing issue:

class Parentclass

That's not the same as the filename ParentClass.class, nor is it the same as the class you're trying to use in ChildClass: class ChildClass extends ParentClass.

Java classnames are case-sensitive, but Windows filenames aren't. If the class had been public, the compiler would have validated that the names matched - but for non-public classes, there's no requirement for that.

The fact that you've ended up with ParentClass.class suggests that at some point it was declared as ParentClass, but then you changed the declared name and when recompiling, Windows just overwrote the content of the current file rather than effectively creating Parentclass.class.

Make sure your declared class name exactly matches the filename. You may well want to delete all your class files before recompiling, just to get out of a confusing state.

people

See more on this question at Stackoverflow