Importing from a Jar file WITHOUT unzipping it

Discussion in 'Java' started by Martyr2, Oct 27, 2008.

  1. Martyr2

    Martyr2 Guest

    Is there anyway I can put a bunch of classes into a jar file (no entry
    point) and simply import that jar file into java applications I am
    working on and access the individual classes through something like
    dot notation? Much like a package, but actually zipped together in a
    jar and my application be able to just pull out classes as needed.

    Any guidance you can provide would be fantastic.
     
    Martyr2, Oct 27, 2008
    #1
    1. Advertising

  2. Martyr2 <> wrote:
    > Is there anyway I can put a bunch of classes into a jar file (no entry
    > point) and simply import that jar file into java applications I am
    > working on and access the individual classes through something like
    > dot notation? Much like a package, but actually zipped together in a
    > jar and my application be able to just pull out classes as needed.


    Adding your jarfile to the classpath of the java applications seems to
    match your expressed wishes to 100%.

    Or did I misunderstand the question?
     
    Andreas Leitgeb, Oct 27, 2008
    #2
    1. Advertising

  3. Martyr2

    Mark Space Guest

    Martyr2 wrote:
    > Is there anyway I can put a bunch of classes into a jar file (no entry
    > point) and simply import that jar file into java applications I am
    > working on and access the individual classes through something like
    > dot notation? Much like a package, but actually zipped together in a
    > jar and my application be able to just pull out classes as needed.
    >
    > Any guidance you can provide would be fantastic.
    >
    >


    What Andreas said. The normal method is to allow the user to install
    and manage the extra libraries themselves. This makes thing easy for
    pros who manage their systems themselves. Note that you have to include
    the name of the jar file itself in the classpath.

    CLASSPATH=/some/path/lib/myLibrary.jar

    Just pointing to the directory won't work, that's for loose classes. I
    think that .zip files will work exactly the same as .jar files, too.

    Also, if your app is a .jar, you have to set the classpath in the
    manifest file. The environment variable is ignored for "java -jar".

    Other solutions:

    1. Get a Java installer, those will install the needed library and set
    the path appropriate.

    2. Use JWS, it'll download the library as needed.

    3. Use OneJar. This is an old and atypical solution, but if you just
    need to package one jar inside of another for a quickie application,
    it'll work.
     
    Mark Space, Oct 27, 2008
    #3
  4. Martyr2

    Arne Vajhøj Guest

    Martyr2 wrote:
    > Is there anyway I can put a bunch of classes into a jar file (no entry
    > point) and simply import that jar file into java applications I am
    > working on and access the individual classes through something like
    > dot notation? Much like a package, but actually zipped together in a
    > jar and my application be able to just pull out classes as needed.
    >
    > Any guidance you can provide would be fantastic.


    You can extract a file from a jar file to memory and load the
    code from memory.

    (I can create code to do so, if you are interested)

    But as said by others: why bother - the normal classloader
    should do it all for you.

    Arne
     
    Arne Vajhøj, Oct 28, 2008
    #4
  5. Martyr2

    Lew Guest

    Mark Space wrote:
    > CLASSPATH=/some/path/lib/myLibrary.jar
    >
    > Just pointing to the directory won't work, that's for loose classes.


    It will if you wildcard the directory:

    -classpath /some/path/lib/*

    Normally one should eschew the CLASSPATH envar.

    --
    Lew
     
    Lew, Oct 28, 2008
    #5
  6. Lew <> wrote:
    > Mark Space wrote:
    >> Just pointing to the directory won't work, that's for loose classes.

    > It will if you wildcard the directory:
    > -classpath /some/path/lib/*


    Are you talking about some new feature of 1.6.0_10 here?

    This year's spring I was attending some one-day Sun event, and the
    speaker talked somewhat self-ironically about various features of
    the "next big update 10": (Rephrased roughly from my bubble-memory)

    "... We knew paths and wildcards for decades, but, the heck,
    why did it take us till 1.6.0_10 to get them together???"
    (Laughter from the audience, mostly programmer-types,
    including me)
     
    Andreas Leitgeb, Oct 29, 2008
    #6
  7. Martyr2

    Lew Guest

    Andreas Leitgeb wrote:
    > Lew <> wrote:
    >> Mark Space wrote:
    >>> Just pointing to the directory won't work, that's for loose classes.

    >> It will if you wildcard the directory:
    >> -classpath /some/path/lib/*

    >
    > Are you talking about some new feature of 1.6.0_10 here?


    <http://java.sun.com/javase/6/docs/technotes/tools/solaris/classpath.html>
    > Class path entries can contain the basename wildcard character *, which is
    > considered equivalent to specifying a list of all the files in the directory
    > with the extension .jar or .JAR.


    It came out before update 10.

    If you're using obsolete or obsolescent versions of Java, YMMV.

    --
    Lew
     
    Lew, Oct 29, 2008
    #7
  8. Martyr2

    Lew Guest

    Lew wrote:
    > Andreas Leitgeb wrote:
    >> Lew <> wrote:
    >>> Mark Space wrote:
    >>>> Just pointing to the directory won't work, that's for loose classes.
    >>> It will if you wildcard the directory:
    >>> -classpath /some/path/lib/*

    >>
    >> Are you talking about some new feature of 1.6.0_10 here?

    >
    > <http://java.sun.com/javase/6/docs/technotes/tools/solaris/classpath.html>
    >> Class path entries can contain the basename wildcard character *,
    >> which is considered equivalent to specifying a list of all the files
    >> in the directory with the extension .jar or .JAR.

    >
    > It came out before update 10.
    >
    > If you're using obsolete or obsolescent versions of Java, YMMV.


    Based on the copyright date, this feature has been in place for two years now.

    --
    Lew
     
    Lew, Oct 29, 2008
    #8
  9. Lew <> wrote:
    >>> Are you talking about some new feature of 1.6.0_10 here?

    >> <http://java.sun.com/javase/6/docs/technotes/tools/solaris/classpath.html>
    >>> Class path entries can contain the basename wildcard character *,
    >>> which is considered equivalent to specifying a list of all the files
    >>> in the directory with the extension .jar or .JAR.

    >> It came out before update 10.
    >> If you're using obsolete or obsolescent versions of Java, YMMV.

    > Based on the copyright date, this feature has been in place for two years now.


    Hmm, you see me confused about whatever I must have missed on that
    event. He did talk about something new in a (back then) future version.

    Maybe the speaker himself mixed it up (didn't know it was there
    already), or I misunderstood him. It's up to you to emphasize
    the latter of these options :)

    I didn't ever try to use the feature, so far, so it might have been
    just under my hands the last couple of years ...

    By that time, iirc, 1.6.0_04 (or up to 06) was current.
     
    Andreas Leitgeb, Oct 29, 2008
    #9
  10. Martyr2

    Arne Vajhøj Guest

    Andreas Leitgeb wrote:
    > Lew <> wrote:
    >> Mark Space wrote:
    >>> Just pointing to the directory won't work, that's for loose classes.

    >> It will if you wildcard the directory:
    >> -classpath /some/path/lib/*

    >
    > Are you talking about some new feature of 1.6.0_10 here?


    Any 1.6 will do.

    Arne
     
    Arne Vajhøj, Oct 29, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    599
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,757
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,819
    Roedy Green
    Feb 14, 2006
  4. Gubatron
    Replies:
    2
    Views:
    587
    Gubatron
    Dec 3, 2008
  5. webcomm
    Replies:
    10
    Views:
    748
    John Machin
    Jan 10, 2009
Loading...

Share This Page