Results 1 to 10 of 10
  1. #1
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default classes in .jar file not seen

    Hi, I added to a project a .jar file containing some classes, but the class where I reffer to them doesn't see them.
    Do I have to explicilty import the classes? Seems it does not recognize the .jar file as a package when trying to do that
    Here is the snapshot, the .jar is added:
    http://postimage.org/image/6fgar7m0x/

    Best Regards

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: classes in .jar file not seen

    You have to import them into the classes you are writing in your project.
    If it doesn't find the packages then either you have the package names wrong, or the jar file is not actually on the build path.

    Ah, looking at the image you have all the classes in the default package. That's not good practice and is probably the root cause of your problem.
    Stick those classes in a package.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default Re: classes in .jar file not seen

    I was thinking about that.
    But this .jar file is supplied as is from the authors, needed for the sedgewick's book 'algorithms with java 4/e'
    Do I have to recompile it??
    p.p.
    I recompiled it
    -unzipped the .jar in a folder
    -navigated to the directory in cmd and typed:
    jar cf stdlib_.jar stdlib_
    //name of .jar, directory of files
    It seems it creates new meta-inf and package folder, no need to do that by myself
    p.p.
    but then again
    import stdlib_.*; - unused import and the classes are not found again!
    Last edited by emosms; 07-31-2012 at 10:59 AM.

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: classes in .jar file not seen

    Do I have to explicilty import the classes?
    You import classes to avoid having to use their proper (full, complete) names in your code. So if I have a class com.etc.myapp.utils.Foo I can, at the time the code is typed and compiled, save myself with an import:

    Java Code:
    import com.etc.myapp.utils.Foo;
    
    // later...
    
    Foo foo = ...
    To underline the point: com.etc.myapp.utils.Foo is the real name of this class. To use the shorthand "Foo" I must have an import so the compiler can tell which Foo I mean.

    -----

    I notice from your image (many won't bother looking, btw - while all appreciate the effort shown by a thorough description) that the classes in the jar archive aren't in a package. You should correct this, and recreate the archive with the classes put into a package (or more). Then, in each class that might use them, import the classes being referenced by their short name.

    Only if all this fails should you consider there might be something wrong with how you incorporated the jar archive into your project.

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: classes in .jar file not seen

    Sedgewick?... That's really disappointing.

    A brief glance at the code on his web site shows examples that use this library. They are all classes written to be in the default package. In that case the problem doesn't arise, but the moment you start writing code for classes that are part of a package you need some way of importing this stuff from the default package, only to find that there is no way to refer to the default package in code...

    Try recompiling the library, since it contains the source code.

  6. #6
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default Re: classes in .jar file not seen

    I recompiled, now there is a package 'stdlib_' inside (just renamed it to keep the original)
    import stdlib_.*; - highlighted as unused import, classes not found again

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: classes in .jar file not seen

    And all the classes were marked as being in that package?
    In other words you edited all the source files for those classes in the jar file?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default Re: classes in .jar file not seen

    The classess inside and their package - still none/default package
    - forgot about that :), I will do that. THX!
    Is there a way to do all the above from within netbeans?
    //I mean, next time I need to do that, I will forget how to compile jar file via cmd and everything...
    - simply right-clickin' and do that in netbeans???

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: classes in .jar file not seen

    If you have the source files, you can drag each one to a package in the NetBeans Project window. That will open a 'Refactor' dialog.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default Re: classes in .jar file not seen

    Yess, yess, like creating new project, drag, compile, smth like that.
    otherwise - dont forget to delete the old compiled files after appending the package name to the source flies, and then compile the new java classes,and THEN make jar file, and finally works! (so effective I am today)
    now i 'just' have to start the actual work

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2012, 04:07 AM
  2. 2 classes in the same file
    By Mapisto in forum New To Java
    Replies: 7
    Last Post: 01-09-2012, 05:25 AM
  3. debug classes in jar file
    By hghayoumi in forum Java Servlet
    Replies: 0
    Last Post: 11-29-2009, 04:54 PM
  4. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 05:56 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
  •