Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Interesting behavior when trying to import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interesting behavior when trying to import" Watch "Interesting behavior when trying to import" New topic
Author

Interesting behavior when trying to import

J Marr
Greenhorn

Joined: Jan 08, 2008
Posts: 5
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

What are you typing exactly on the command line to compile the two pieces of code?

This sounds like a classpath problem, or a problem with the directory structure or file names of your Java source or class files.

First, if you put classes in a package, then they have to be in a directory structure that matches the package name. So your classes Rhino, Bird and Animal, which are in the pacakge practice.animalpackage, should be in a directory structure that looks like this:


C:\Project
|_ practice
   |_ animalpackage
      |_ Rhino.java
      |_ Bird.java
      |_ Animal.java

You should compile them from the base of the package:

C:> cd \Project
C:\Project> javac practice\animalpackage\*.java

Then, when you compile your class AnimalTester, you should include the base directory in the classpath:

C:\Somewhere> javac -cp C:\Project;. AnimalTester.java


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Welcome back (to Java Land) "J",

Use Code Tags


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
J Marr
Greenhorn

Joined: Jan 08, 2008
Posts: 5
Jesper:

Thank you for your reply. As per your suggestion I checked my directory structure and did not have it quite right (I was also using TextPad to compile the source and so to eliminate that as a source of confusion I went directly to the command line). So I copied your suggested directory structure and commands exactly as you wrote them, and began again from scratch.

I set up the following directory structure: C:\project\practice\animalpackage and into this directory I copied the java source files for Rhino, Bird and Animal. Now I compiled them with the following command, which executed fine:

C:\project>javac practice\animalpackage\*.java

So far so good. No problems. And the class files ended up where they are supposed to be, right inside C:\project\practice\animalpackage

Now I returned to "C:\Somewhere" and executed the following, copying your instructions (albeit, from the "Somewhere" which is as you see it below):

C:\Documents and Settings\Alex\Desktop\javapractice>javac -cp c:\project;. AnimalTester.java

Again, no problems. Everything's OK so far. And the version of AnimalTester.java that I'm compiling is version #2 from above, i.e., it contains the following import statement: import practice.animalpackage.*;

Now when I try to run AnimalTester, I get the following result:

C:\Documents and Settings\Alex\Desktop\javapractice>java AnimalTester
Exception in thread "main" java.lang.NoClassDefFoundError: practice/animalpackage/Rhino
at AnimalTester.main(AnimalTester.java:7)

Clearly I'm missing something simple. That's the price I'm paying for a year out of the saddle!

Best Regards,

Joe
J Marr
Greenhorn

Joined: Jan 08, 2008
Posts: 5
Bill:

Thanks, and good to be back! "J" = Joe.

Best Regards,

Joe
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You have to also tell the runtime where the animal classes are:

java -cp c:\project;. AnimalTester


[Jess in Action][AskingGoodQuestions]
J Marr
Greenhorn

Joined: Jan 08, 2008
Posts: 5
Thank you very much, Dr. Friedman-Hill! Works just fine when I informed the runtime accordingly.

Best Regards,

Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting behavior when trying to import