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 7 of 7

Thread: Javatip Dec 18, 2010 - Eclipse User Libraries

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Javatip Dec 18, 2010 - Eclipse User Libraries

    Introduction

    In this tip I will be focusing on how to create a user library in Eclipse. User libraries help you to quickly setup a new project with a library pack you got from some source. Examples of library packs are The Lightweight Java Gaming Library (LWJGL), ANother Tool for Language Recognition (ANTLR), and Jython.

    Difficulty: Easy. This is a fairly easy process, and is actually easier (as well as more robust) than other methods for setting up eclipse to use a library. For the purposes of this tip, I'll be focusing on doing this process with LWJGL, but the same process can be applied to any library (even ones you make yourself).

    Basic Procedure

    A. Go into the Eclipse preferences > Java > User Libraries. Click on "New.."

    user_libraries01.jpg

    B. Choose a name for your user library. Unless you know what you're doing, I suggest leaving the "System Library" checkbox unchecked. Push "OK".

    add_library2.jpg
    user_libraries02.png

    C. Your library should now show up in the list of user libraries. Now we need to add the jar files and/or source/javadoc associated with this library. Select your library and click "Add JARs...". Navigate to the location where you put the library. As a general rule, you SHOULD NOT put these JAR files into the same directory as the JRE or JDK! Normally I create a directory for all my Java executables and libraries, with a sub-directory for the JRE install, a sub-directory of the JDK install, and other sub-directories for any libraries you want).

    user_libraries03.jpg
    library_path.jpg
    library_jars.jpg

    D. For each JAR file, there are 4 additional parameters you can attach to the JAR: Source location, Javadoc location, Native library location, and access rules. For this tutorial I will ignore the last parameter. The source and Javadoc locations are always option, however I would strongly recommend including the Javadoc if it's available. The Native library location is the location of an JNI libraries used by that JAR. This is not an optional field, and must be included if the JAR requires one (see the library documentation).

    library_jars2.png

    Attaching Source location

    This will allow you to step into the library's source code when you're debugging. In general, you won't need to do this (unless it's your library, or you're debugging the library).

    E. Select "Source attachment" for a JAR, and click "Edit...".

    source01.jpg

    F. Choose a valid path to the location of the source for that JAR. Click "OK" when done.

    source02.png

    Attaching Javadoc location

    This allows Eclipse to give you helpful Javadoc pop-ups when you try to use classes/members of the the Jar. I would highly suggest you add Javadocs for your libraries.

    G. Select "Javadoc location" for a JAR, and click "Edit...".

    H. You have a few options for choosing where the Javadoc is located. If the library has online Javadoc, you can even reference that. Below are screenshots of what these paths might look like for both local Javadoc and online Javadoc.

    local_javadoc.jpg

    online_javadoc.jpg

    Attaching Native Library Location

    I. Select "Native Library Location" and click "Edit...".

    J. Enter a valid path for the native library location. Click "OK"

    native_library.png

    Using a User Library

    Now that you have the user library all setup, you shouldn't need to touch it ever again (unless you're updating the library). All you need to do is tell eclipse that you want to include that library into the build path. This can be done either when you first create the project, or can be done after you create the project.

    Modifying build path on New Project

    1. Create a new Java Project. Enter in the name of the project and instead of clicking finish, click "Next>".

    2. Click on the "Libraries" tab and choose "Add Library..."

    add_library.jpg

    3. Choose "User Library" and click "Next>"

    add_library2.jpg

    4. Check the libraries you want to use and click OK.

    add_library3.jpg

    5. Finish creating your Java project by clicking "Finish"

    Modifying the build path on an existing project

    1. Right click on the project in question and choose "Build Path" > "Add Libraries...". Follow steps 3 and 4 in the section above to add the library(s) to your project.

    add_library4.jpg

    Conclusion

    Hopefully this will simplify the process of using external libraries for your projects. This method makes it extremely easy for you to manage a specific library and helps you maintain all your project libraries in one convenient location.

    Happy Coding
    Last edited by helloworld922; December 19th, 2010 at 04:25 PM.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    Hello,

    I've follog the steps above, but still I cannot use the classes of the added libraries. Do I have to do anything else in my java promramm, p ex "Import" or anything else?

    Thanks

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    Once you've added the library, you can use the class as you could any other class (yes, you will need to import the classes).

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    Hello,

    That I've done (from Eclipse) is right click the java project and then in "Properties" I clicked "Java Build Path" in there I selected the label Projects then "Add" and in this option I added
    the project where the classes are.

    Now all the classes of that added project can be seen in my java program without any import.

    Is it ok? will I have a problem? is there a best way to do it?


    Thank you for you kind and fast answer

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    hmm, if I understand you correctly you added all the classes of the library into your project? Or did you just add the user library you created? While technically either method would work, the second method is by far the simpler method.

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    Hello,

    I've just added the 3 classes I created .


    BR
    Luis

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javatip Dec 18, 2010 - Eclipse User Libraries

    Thanks. I had been struggling with using Joda-Time with Eclipse, and this post helped me out.

Similar Threads

  1. Replies: 21
    Last Post: December 27th, 2013, 02:33 AM
  2. How to add JSF libraries to eclipse?
    By tien1504 in forum Java IDEs
    Replies: 5
    Last Post: October 24th, 2012, 04:32 PM
  3. Java Tip Nov 20, 2010 - Spline Interpolation
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: November 20th, 2010, 11:44 AM
  4. Java Tip Jul 5, 2010 - [Eclipse IDE] Navigating through code
    By helloworld922 in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: July 5th, 2010, 06:28 AM
  5. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 09:32 AM