Results 1 to 10 of 10
  1. #1
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default Creating/Using Class Libraries in NetBeans

    I have created a Class Library project with NetBeans, I am trying to understand how to use that Class Library in a test program.

    Lets say the Class Library is called MyClassLib, in package MyClassLib, and contains one class called TestClass. When I build the class library this gives me an executable JAR file named MyClassLib in the \dist folder for the MyClassLib project.

    I created a new project for my test application. I copied the MyClassLib jar file to the project directory.
    I'm assuming that the test program needs to have the following import:
    import MyClassLib.TestClass;

    However, this gives me the error "Package MyClassLib does not exist", which indicates I am doing something stupid here.

    What is the right way to use a class library like this? What files need to be moved to the test program project and where should they go?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In NetBeans there is an option to add external libraries. Right-click on your project and click libraries. From there you can add jar files into your project.

    Or else you've add those jar files into your classpath.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    When I build the class library this gives me an executable JAR file
    Building a library shouldn't produce an executable jar.
    I copied the MyClassLib jar file to the project directory.
    Don't.
    I'm assuming that the test program needs to have the following import:
    import MyClassLib.TestClass;
    Wrong assumption.
    What files need to be moved to the test program project
    None.

    Open the project properties for the new project, the one that has to use the library previously created, and add the library via Add JAR/Folder.

    db

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Building a library shouldn't produce an executable jar.

    Don't.

    Wrong assumption.

    None.

    Open the project properties for the new project, the one that has to use the library previously created, and add the library via Add JAR/Folder.

    db
    I would argue, unless you're using something like Maven, that you do copy across libraries that are used within your project. I'm thinking in terms of source control and project management. I'd be a bit peeved if, on checking out a project, I found it didn't have all the libraries I needed in its lib directory for me to actually build the thing.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    I'd be a bit peeved if, on checking out a project, I found it didn't have all the libraries I needed in its lib directory
    Agreed, but the question was about how to use a library in NetBeans.

    NetBeans also takes care of copying the library jar to the /dist/lib folder.

    I would guess the OP needs a bit more time at this level before moving on to the intricacies of source control.

    db
    Last edited by DarrylBurke; 11-25-2010 at 11:07 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    I suppose so.
    I'd still be tempted to get them used to the idea of self contained projects...but that might just be me.

  7. #7
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks, that worked!

    Of course, this leads to yet another question. I have added JavaDoc comments to my class library. In the project that uses the class library, as I type in NetBeans it displays the "intellisense" info on the classes/methods in the library, but gives only the basic format, not my JavaDoc information - displays the following:
    Javadoc not found. Either Javadoc documentation for this item does not exist or you have not added specified Javadoc in the Java Platform Manager or the Library Manager.

    What should I do to allow NetBeans to see the JavaDoc for my class library?

    Thanks again.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    It looks rather like your doc comments may not be well formed. Could you post one small class with all doc comments for review?

    Another way to check this is to generate the javadoc for the library project and watch for any errors. Right click the project node and select Generate Javadoc.

    Another possibility is that you copied the library jar to another location and added the copy. See below.

    @Eranga: I just checked this, in NetBeans it isn't necessary that the JavaDoc for the library be available in html form, it seems to pick it up directly from the source code even when the library project is closed, but only if the library jar has been added from its default location in project/dist.

    db

  10. #10
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    I had copied the jar file to the new project folder and added the copy. As you said, the JavaDocs do work if I add the jar file directly from the /dist folder in the project for the class library.

    Eventually I want to distribute this class library and I would like the JavaDoc information to be available to the user. Can this be done from the jar file alone or will this require generation of the HTML? What is the typical/correct way to distribute a class library - as a jar file or something else?

Similar Threads

  1. Problem in importing jar file in libraries of netbeans
    By dataprofiling in forum AWT / Swing
    Replies: 1
    Last Post: 03-30-2010, 11:30 AM
  2. Sharing Project Libraries in NetBeans
    By Ajenjo in forum NetBeans
    Replies: 0
    Last Post: 03-15-2010, 09:38 AM
  3. Replies: 13
    Last Post: 08-26-2009, 04:08 AM
  4. Location of java class libraries on Sun
    By phudgens in forum New To Java
    Replies: 3
    Last Post: 02-05-2009, 04:45 AM
  5. Creating a Session Ejb in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 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
  •