Hello,
I am self-teaching java to myself with a library book. The book I'm working through suggests that I create a package called BookPack, it defines the following class:
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(); } }
Then the book tells me to create a package called BookPackB, which defines the main() class. The code is listed below:
package BookPackB; // Use the Book Class from BookPack. public class HSUseBook { 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("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[i].show(); } }
I have compiled this code in a NetBeans IDE and I didn't believe the errors, so I ran it from the command prompt. I will tell you what I did at the command prompt because I think it has something to do with the location of the directories for each package/class. When creating "projects" in the NetBeans IDE, the software creates a package and directory named after the main() class, I have tried and tried to make these simple pieces of code work in the NetBeans IDE, but to no avail. I have gone the the command prompt to see if I can learn something there and I did learn some valuable things. By the way do professional programmers use a source code editor and command prompt or an IDE, or a bit of everything?
The compiled code should output the following I am going to list the first object books[0] output, to list the rest seems trivial:
I get none of the output I desire, not even the generation of a .class file after compiling. Let me tell you what I did.Java: A Beginner's Guide Schildt 2005
From the command prompt, I used javac java\BookPackB\HSUseBook.java, HSUseBook. In case you would like to know, I have the package BookPack with book class at C:\java\BookPack\Book.java Should one package be in a higher directory than another?
After the "javac" command listed above, I get an error at this syntax in the code just below main(), BookPack.Book books[] = new BookPack.Book[5]; where BookPack is underlined. The error syntax for this statement is:
The same errors exist for the subsequent declarations for book[] objects in the main(), for example an error for the broken statement books[0] = new BookPack.Book("Java: A Beginner's Guide",....., ....), where BookPack is underlined, says that BookPack does not exist.
After running the "java" command in command prompt java java\BookPackB\HSUseBook.java I get the following errors:
This lengthy error report maybe better than the IDE's error report but I can't make sense of it all. But to me it says first that it can not find a class definition in main(), then it lists a whole pile of packages that are affected by or are possible fixes to the "missing class" and finally it states that it could not find the main class in C:\java\BookPackB\HSUseBook.java.Exception in thread "main" java.lang.NoClassDefFoundError: java\BookPackB/HSUseBook Caused by: java.lang.ClassNotFoundException: java\BookPackB/HSUseBook at java.net.URLClassLoader$1.run<URLClassLoader.java:202> at java.security.AccessController.doPrivileged<Native Method> at java.net.URLClassLoader.findClass<URLClassLoader.java:190> at java.lang.ClassLoader.loadClass<ClassLoader.java:306> at sun.miscLauncher$AppClassLoader.loadClass<ClassLoader.java:247> Could not find main class: java\BookPackB.HSUseBook. Program will exit.
Well I am sorry for the length, hopefully, this is just a simple fix. It should be it is in a book, but with my luck it will be the only faulty example.
Cheers