Good evening to all. I'm new to JavaRanch and just getting back into
Java coding after about a year away.
I'm getting an interesting error message from an attempt to import a package that I compiled previously.
Here's the first version of some small code that compiles successfully and that imports three previously compiled classes by full name:
import practice.animalpackage.Rhino;
import practice.animalpackage.Bird;
import practice.animalpackage.Animal;
public class AnimalTester
{
public static void main(
String[] args)
{
Rhino x = new Rhino();
Bird y = new Bird();
System.out.println( "x says: " + x.makeSound() );
System.out.println( "y says: " + y.makeSound() );
System.out.println( "x is a mammal: " + x.isMammal() );
System.out.println( "y is a mammal: " + y.isMammal() );
System.out.println( "y is a bird: " + y.isBird() );
}
}
And now, here's the second version of the above code that does not compile, along with the error message that's generated:
import practice.animalpackage.*;
public class AnimalTester
{
public static void main(String[] args)
{
Rhino x = new Rhino();
Bird y = new Bird();
System.out.println( "x says: " + x.makeSound() );
System.out.println( "y says: " + y.makeSound() );
System.out.println( "x is a mammal: " + x.isMammal() );
System.out.println( "y is a mammal: " + y.isMammal() );
System.out.println( "y is a bird: " + y.isBird() );
}
}
Error message:
C:\Documents and Settings\Alex\Desktop\javapractice\AnimalTester.java:7: cannot access Rhino
bad class file: .\Rhino.java
file does not contain class Rhino
Please remove or make sure it appears in the correct subdirectory of the classpath.
Rhino x = new Rhino();
^
1 error
Tool completed with exit code 1
So question is, why does the first one compile and the second one does not? It appears to me that the classloader isn't recursing into the package directory structure: classes Rhino, Bird and Animal are all present where they are supposed to be, as evidenced by the fact that the first version of the code picks them up. I also believe I've been careful with the classpath variables, etc. Any comments would be welcome as to why the second version does not allow the "type-import-on-demand" statement, and thus, does not pick up on the classes that are already present in the package directory structure.
Best Regards,
Joe