Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple package problem, package does not exist error

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple package problem, package does not exist error

    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:
    Java: A Beginner's Guide 
    Schildt
    2005
    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.

    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:
    java\BookPackB\HSUseBook.java:15: package BookPack does not exist BookPack.Book books[] = new BookPack.Book[5]

    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:
    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.
    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.

    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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple package problem, package does not exist error

    TL;DR

    It seems you have a classpath issue. Do a bit of reading to get a better understanding.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Simple package problem, package does not exist error

    That's a lot of questions!
    Always use Java Code Conventions:
    NO TITLE
    When Java programmers look at your code, they'll be looking at an UpperCamelCase label and thinking "Hmmm, class...". Code that uses UpperCamelCase for package names is a Twilight Zone for people who spend all day reading Java code.

    It's a good idea to use packages just to keep your classes from interfering each other. Simple package names are particularly handy when you're learning when you might have several versions of 'Book' that increase in functionality as you progress. Packages can keep this week's code separate from last week's code. I would recommend package names like "labweek1" for students. "package bookpack" seems not unreasonable. If you're working on a small project and there's no chance of name-collisions (such as a 'Book' class that models a library book and a 'Book' class that models a betting session) then there's no good reason to use more than one package. If you use only one package, you may also defer learning about 'import' statements and qualifying your classes with package prefixes such as 'bookpack.Book' - you can use 'Book' on its own within the same package.

    What's the name of the programming guide you're working from? We could add it to some sort of 'avoid' list. The deviation from Java Code Conventions is a bad start.

    Junky's right - you seem to have fiddled your way into a classpath problem. It's very difficult to get the same results compiling by hand as it is compiling within an IDE. If you mix the two, it's not unusual to get into a state where you have mis-matched class (generated by the compiler) files, and nothing works. Stick with the IDE. As a time-served professional programmer who has never used one, I regret now not being able to tick those boxes in recruiters' forms.

    That last stack trace doesn't have much useful information in for you, except for the top line, and even that takes some familiarity to understand. When you start a JVM on the command line with 'java', the argument you pass in is the name of the class that the JVM should load first to invoke its main(String[]) method. If that class is in a package, you must provide the fully-qualified class name, such as 'java bookpack.BookApp' (if your application starts from a BookApp class in package 'bookpack'). When you compile your source files, javac (the Java compiler) writes the output .class file into a directory tree with one directory per dot-separated label in your package names. On my (Linux) PC, 'javac BookApp.java' would create the directory bookpack (if it did not already exist) and then write BookApp.class into the directory bookpack. To load that class into the JVM, I would type 'java bookapp.BookApp', and the JVM would look in the 'bookapp' directory for the file BookApp.class.

    The package-to-directory names feature is important because different Operating Systems have different ways of formatting directory paths. Starting an app "java package.hierarchy.StartClass" means you can use the same command line on different operating systems and the JVM that's native to that OS will 'know' how to create a locally-valid filepath that it can use to locate the file containing the class bytecode.

    A long answer. One question per thread next time!

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple package problem, package does not exist error

    Hello all,

    I solved the problem using a combination of things you all mentioned. First, I changed the classpath variable in the command prompt using:
    C:\>set CLASSPATH=.;C:\java
    Then I invoke javac using:
    C:\>javac java\BookPackB\HSUseBook.java
    Then I invoke java using:
    C:\>java BookPackB.HSUseBook
    The code compiles and provides the desired output! Thank you.

    Sean4U,
    I read your post on naming conventions amidst the other valuable information. The book, "Java a beginners guide" by Herbert Schildt uses the code I have posted. In the future, I will change all future work so that package BookPackB; is package bookpackb;. I haven't gotten to "this" code yet, I was just anxious to get this running.

    You mentioned using the IDE if possible. Well I am going to give it another set of attempts. Thank you.

Similar Threads

  1. package config problem?
    By droidtech1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 29th, 2011, 11:01 AM
  2. Package does not exsist
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 31st, 2011, 03:06 PM
  3. PROBLEM CHECKING IF RECORDS EXIST OR DO NOT EXIST IN DATABASE
    By jimmyb0206 in forum JDBC & Databases
    Replies: 1
    Last Post: April 10th, 2011, 09:18 AM
  4. Is it possible to package Java
    By UnderWater2 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 2nd, 2010, 01:31 AM
  5. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 11:50 AM