3 Replies Latest reply: May 29, 2013 12:53 PM by gimbal2 RSS

    import a package

    francy77
      Hi all,
      i' m try to compile and execute the following 2 files:

      file Animal.java that contain:
      package pluto;
      public class Animal {
          public static void testClassMethod() {
              System.out.println("The class" + " method in Animal.");
          }
          public void testInstanceMethod() {
              System.out.println("The instance " + " method in Animal.");
          }
      }
      file Cat.java that contains:
      import pluto.*;
      public class Cat extends Animal {
          public static void testClassMethod() {
              System.out.println("The class method" + " in Cat.");
          }
          public void testInstanceMethod() {
              System.out.println("The instance method" + " in Cat.");
          }
      
          public static void main(String[] args) {
              Cat myCat = new Cat();
              Animal myAnimal = myCat;
              Animal.testClassMethod();
              myAnimal.testInstanceMethod();
          }
      }
      from the shell:
      C:\Documents and Settings\francesco\prove_java>javac Animal.java
      
      C:\Documents and Settings\francesco\prove_java>
      the file Animal.class has been created

      Now i try to compile the file Cat.java
      C:\Documents and Settings\francesco\prove_java>javac -classpath . Cat.java
      Cat.java:1: package pluto does not exist
      import pluto.*;
      ^
      Cat.java:2: cannot access Animal
      bad class file: .\Animal.class
      class file contains wrong class: pluto.Animal
      Please remove or make sure it appears in the correct subdirectory of the classpath.
      public class Cat extends Animal {
                               ^
      it tells me that the package pluto does not exist, and that isn't true it exist in the file Animal.java, someone can explain to me where I'm wrong?

      thanks
      F.
        • 1. Re: import a package
          rp0428
          >
          it tells me that the package pluto does not exist, and that isn't true it exist in the file Animal.java, someone can explain to me where I'm wrong?
          >
          No - that is backwards.

          Packages do not exist in class files - class files exist in packages.

          Do you have a folder named 'pluto'? Did you put your class file 'Animal.java' in it?

          Why isn't your 'Cat' class in a package?

          See how to use packages in the Java Tutorial
          http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html

          You will have far fewer problems if you use standard practices and do things the way that most others do them.

          1. Create a folder hierarchy that matches your package structure
          2. Put ALL class files into an appropriate package and folder
          • 2. Re: import a package
            francy77
            thanks. import doesn't import class but it import files inside a directory (that are class). Cat is not in a pakage just because it was an example, from

            http://docs.oracle.com/javase/tutorial/java/IandI/override.html

            thanks.
            • 3. Re: import a package
              gimbal2
              That is an example of a class yes, but not of a complete source file. They are commonly referred to as snippets - parts of code. You are supposed to make them complete yourself, which means you need to know what you're doing. Which means you need to study packages and the classpath.