4 Replies Latest reply: Dec 19, 2008 6:40 AM by 843785 RSS

    Compiling programs in different directories (packages)

      I'm studying Java from the book, Java: A Beginner's Guide Fourth Edition. I've come across a hurdle regarding compiling programs where classes are called from other files (packages). I can compile without error a file that doesn't include 'main()' which resides in it's own directory, "BookPack". I can't compile the second file which contains the 'main()' function. This file residing in another directory by the same name as it's package, "BookPackB". The error reported is "..... package BookPack.Book(..." does not exist.

      For completeness here are the two files (page 308 from the book).
      // Book recoded for public access.
      package BookPack;
      public class Book {
        private String title;
        private String author;
        private int pubDate;
        // Now public.
        public Book(String t, String a, int d) {
          title = t;
          author = a;
          pubDate = d;
        // Now, public.
        public void show() {
      // This class is in package BookPackB
      package BookPackB;
      // Use the Book Class from BookPack.
      class UseBook {
        public static void main(String args[]) {
          BookPack.Book books[] = new BookPack.Book[5];
          books[0] = new BookPack.Book("Java: A Beginner's Guide", "Schildt", 2007);
          books[1] = new BookPack.Book("Java: The Complete Reference", "Schildt", 2007);
          books[2] = new BookPack.Book("The Art of Java", "Schildt and Holmes", 2003);
          books[3] = new BookPack.Book("Red Storm Rising", "Clancy", 1986);
          books[4] = new BookPack.Book("On the Road", "Kerouac", 1955);
          for(int i=0; i<books.length; i++) books.show();
      I would appreciate some simple guidance in the steps to compile the above files.
        • 1. Re: Compiling programs in different directories (packages)
          Tell us how your directory structure looks like. Is there a common base directory? Where do the .java files and the .class files reside?
          • 2. Re: Compiling programs in different directories (packages)
            My base direcory is "Module-08" and I have two sub-directories, "BookPack" and "BookPackA". The first file with the class Book resides in the BookPack directory and the other file containing 'main()' resides in the BookPackA directory.
            • 3. Re: Compiling programs in different directories (packages)
              superjacent wrote:
              My base direcory is "Module-08" and I have two sub-directories, "BookPack" and "BookPackA".
              The first file with the class Book resides in the BookPack directory and the other file containing 'main()' resides in the BookPackA directory.
              Which ones? The .java files? or the .class files?

              There are two possible ways to compile such structures: Have the .class files be next to the .java files (that's the easier one) or have a parallel directory structure for the .class files that mirrors the source structure (that's the nicer one but a tiny bit harder to set up).

              I'll describe the first one here.

              Your goal is for your directories + content to be like this:


              1.) Go to Module-08
              2.) compile Book.java using this command:
              javac -cp . BookPack/Book.java
              3.) compile UseBook.java using this command:
               javac -cp . BookPackB/UseBook.java
              The "-cp ." means "Use the current directory as the Classpath".

              Note that your package names should be lower-case, that's a convention and it would be better to follow it.
              • 4. Re: Compiling programs in different directories (packages)
                Thanks, your solution worked. Sorry for being vague re - location of both .java and .class files. They do reside together in their respective directories, as per your assumption.

                As regards the package names I'll take that on board; I'm surprised the book isn't following that convention.