Re: Java package problem
dkra wrote:
x-no-archive: yes
Hello,
I tried to put together a practice package in Java. I'm using JavaSE2 v. 5
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)
That's the runtime environment, but I also have the JSE2 v. 5 development
enviroment.
Here's the problem:
I created a .java file called BookDemo.java
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// A short package demonstration. p. 304 text
package BookPack;
class Book {
private String title;
private String author;
private int pubDate;
Book(String t, String a, int d) {
title = t;
author = a;
pubDate = d;
}
void show() {
System.out.println(title);
System.out.println(author);
System.out.println(pubDate);
System.out.println();
}
}
class BookDemo {
public static void main(String args[]) {
Book books[] = new Book[5];
books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2005);
books[1] = new Book("Java: The Complete Reference", "Schildt", 2005);
books[2] = new Book("Java: A Beginner's Guide", "Schildt and Holmes", 2003);
books[3] = new Book("Red Storm Rising", "Clancy", 1986);
books[4] = new Book("On the Road", "Kerouac", 1955);
for(int i = 0; i < books.length; i++) books[i].show();
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and placed it in a directory called BookPack. I compiled the source code
in the BookPack directory, then "stepped outside" the directory to run
java BookPack.BookDemo
to run it. It ran. So far so good.
However, the next step did not work. I typed "public" in several places
in the Book class part of this file, to make it accessible and public.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// A short package demonstration. p. 304 text
package BookPack;
public class Book {
private String title;
private String author;
private int pubDate;
public Book(String t, String a, int d) {
title = t;
author = a;
pubDate = d;
}
public void show() {
System.out.println(title);
System.out.println(author);
System.out.println(pubDate);
System.out.println();
}
}
class BookDemo {
public static void main(String args[]) {
Book books[] = new Book[5];
books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2005);
books[1] = new Book("Java: The Complete Reference", "Schildt", 2005);
books[2] = new Book("Java: A Beginner's Guide", "Schildt and Holmes", 2003);
books[3] = new Book("Red Storm Rising", "Clancy", 1986);
books[4] = new Book("On the Road", "Kerouac", 1955);
for(int i = 0; i < books.length; i++) books[i].show();
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I saved the file.
I created a new file, in the same directory, called UseBook.java.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This class is in package BookPackB. text p. 308
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", 2005);
books[1] = new BookPack.Book("Java: The Complete Reference",
"Schildt", 2005);
books[2] = new BookPack.Book("Java: A Beginner's Guide", "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[i].show();
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This file would not compile.
Compiler complained that the package BookPack did not exist.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UseBook.java:7: package BookPack does not exist
BookPack.Book books[] = new BookPack.Book[5];
^
UseBook.java:7: package BookPack does not exist
BookPack.Book books[] = new BookPack.Book[5];
^
UseBook.java:9: package BookPack does not exist
books[0] = new BookPack.Book("Java: A Beginner's Guide", "Schildt", 2005);
^
UseBook.java:10: package BookPack does not exist
books[1] = new BookPack.Book("Java: The Complete Reference",
"Schildt", 2005);
^
UseBook.java:11: package BookPack does not exist
books[2] = new BookPack.Book("Java: A Beginner's Guide", "Schildt and
Holmes", 2003);
^
UseBook.java:12: package BookPack does not exist
books[3] = new BookPack.Book("Red Storm Rising", "Clancy", 1986);
^
UseBook.java:13: package BookPack does not exist
books[4] = new BookPack.Book("On the Road", "Kerouac", 1955);
^
7 errors
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Could someone please explain to me why this is not working? What needs to
be done to make the UseBook.java program access the BookPack package? Is
it a Java2 v. 1.4 versus Java2 v. 1.5 thing?
Thanks in advance.
-- dkra
You have to be in the directory above your package to run a program in
the package.
--
Knute Johnson
email s/nospam/knute/