Results 1 to 3 of 3
  1. #1
    farmer is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Package Problems in NetBeans IDE

    Hello,

    I am working through this book from the library to teach java to myself. About two weeks ago, I hit the package and interface chapter and things haven't gone very smoothly. I posted to the NetBeans area of the forum because after trying to mess with packages and class files in a NetBeans IDE project/projects for 10 days, I had no other option but to use the command prompt. Let me tell you, it wasn't to bad overall and the code compiled eventually after properly setting CLASSPATH. As powerful as the command prompt may be, I prefer to use an IDE. I have been working with the NetBeans IDE for about 10 weeks now, so I would like to stick with it but I need your help. Here is what I have done in the NetBeans IDE:

    The book told me to create a package called bookpackb containing main(). Here is the code:
    Java Code:
    package hsusebook.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();
        
        }
    }
    The book told me to create a package called bookpack containing the class Book. Here is the code:
    Java Code:
    package hsusebook.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() {  
        System.out.println(title);  
        System.out.println(author);  
        System.out.println(pubDate);   
        System.out.println(); 
      }  
    }
    Also note that the package declarations contain the package hsusebook, this package is created for the main class when the project is first built. All other packages that are possible to create (from my meager knowledge) are created as subpackages. I hope this is OK, when I built/compiled with command prompt, the packages bookpack and bookpackb were in the same directory called "java". The packages for the NetBeans IDE project HSUseBook are on a similar level for the IDE.

    Here is the path containing main():
    Java Code:
    C:\...\NetBeansProjects\HSUseBook\src\hsusebook\bookpackb\HSUseBook.java
    Here is the path to the other file:
    Java Code:
    C:\...\NetBeansProjects\HSUseBook\src\hsusebook\bookpack\Book.java
    When I run the project I get the following error:
    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 
    package bookpack does not exis 
    at hsusebook.bookpackb.HSUseBook.main(HSUseBook.java:15)
    Java Result: 1
    Line 15 from the error above corresponds to this syntax below main():
    Java Code:
    bookpack.Book books[] = new bookpack.Book[5];
    I expect this compiled code to use the first object in printing:
    Java Code:
    Java: A Beginner's Guide
    Schildt
    2005
    I wonder what book I am using. Well I hope someone can help me!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Your classes (as you've noted) are in hsusebook.bookpackb and hsusebook.bookpack, but you are referring to the Book as bookpack.Book, which is not one of those packages. So the compiler cannot find that package.

    Use hsusebook.bookpack.Book.

    This is simply because you are learning using an IDE rather than the command line and a basic text editor (or possibly Notepad++). I would suggest putting Netbeans to one side until you've done the tutorials in that book. It will help you understand what it is the IDE is actually doing for you.

  3. #3
    farmer is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Your classes (as you've noted) are in hsusebook.bookpackb and hsusebook.bookpack, but you are referring to the Book as bookpack.Book, which is not one of those packages. So the compiler cannot find that package.

    Use hsusebook.bookpack.Book.

    This is simply because you are learning using an IDE rather than the command line and a basic text editor (or possibly Notepad++). I would suggest putting Netbeans to one side until you've done the tutorials in that book. It will help you understand what it is the IDE is actually doing for you.
    Tolls,

    It works following your suggestions on the location of the classes, i.e. using hsusebook.bookpack.Book. In this manner, all of the packages are defined. As for the IDE, at first it took some time learning the Command Prompt to compile and run java code, so I ditched it for the IDE because I wasn't getting anywhere. There was just to much information and excitement (to get something going) when I first opened the book. When I hit this chapter on Packages and Interfaces, I hit a wall and couldn't get any thing to work. After failure with the IDE, I went back to the Command Prompt and started with "Hello World", then worked another example from the book, then went to the "Package Example" and it worked. When I got back too the IDE, I didn't put together what you suggested (as you hadn't suggested it yet). I was too hung up on how to build packages in a project and what levels the directories should be on. If it wasn't that, it was is my whether or not my classpath in the IDE was correct. In reading on classpath in the IDE it seems somewhat different than that of the system variable CLASSPATH set in the command prompt. It mostly had to do with building Free Form Project and running Ant Script, so I was confused. Your solution, makes obvious sense now and I learned quite a bit about things not related to the solution. This is the learning process. Thank you.

Similar Threads

  1. Problems with RMI and NetBeans
    By Moons in forum Advanced Java
    Replies: 6
    Last Post: 12-11-2010, 03:20 AM
  2. Replies: 13
    Last Post: 12-06-2010, 05:57 AM
  3. NetBeans losing package information when crashing
    By Kethas in forum New To Java
    Replies: 4
    Last Post: 03-03-2010, 05:38 AM
  4. Problems with package name
    By riffla in forum Eclipse
    Replies: 1
    Last Post: 07-16-2009, 06:51 PM
  5. Problems with NetBeans
    By gabriel in forum NetBeans
    Replies: 2
    Last Post: 11-07-2007, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •