• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Interesting behavior when trying to import

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome back (to Java Land) "J",

Use Code Tags
 
J Marr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill:

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

Best Regards,

Joe
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to also tell the runtime where the animal classes are:

java -cp c:\project;. AnimalTester
 
J Marr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Dr. Friedman-Hill! Works just fine when I informed the runtime accordingly.

Best Regards,

Joe
 
That feels good. Thanks. Here's a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic