Add *.jars to *.jars possible/recommendend? Eclipse setup for this?

Discussion in 'Java' started by Ulf Meinhardt, Feb 3, 2010.

  1. Assume I want to create a new program and to pack everything into a *.jar archive.
    To run the program one (or more) additonal external *.jar is required.

    Ok, the potential user could install the two *.jar files independently and add them
    to his CLASSPATH. This is somehow uncomfortable. I would like to deploy/provide simply one
    *.jar with everything needed inside.

    So I have to add the second, external jar into the "main" first jar as well.

    Is this procedure possible/unusual/recommended?

    How do I do this from the command line ?

    How do I tell Eclipse to not only reference the external *.jar lib but ADD them physically
    into the target *.jar?

    As far as I know simply adding them on the Properties->Java Build Path->Libraries dialog
    does NOT automatically add them into the target *.jar but only references it.

    Ulf
     
    Ulf Meinhardt, Feb 3, 2010
    #1
    1. Advertising

  2. Ulf Meinhardt

    Nigel Wade Guest

    Re: Add *.jars to *.jars possible/recommendend? Eclipse setup forthis?

    On Wed, 03 Feb 2010 11:17:16 +0000, Ulf Meinhardt wrote:

    Don't multi-post. I've just realized this has already been answered in
    the other NG.

    > Assume I want to create a new program and to pack everything into a
    > *.jar archive. To run the program one (or more) additonal external *.jar
    > is required.
    >
    > Ok, the potential user could install the two *.jar files independently
    > and add them to his CLASSPATH. This is somehow uncomfortable. I would
    > like to deploy/provide simply one *.jar with everything needed inside.
    >
    > So I have to add the second, external jar into the "main" first jar as
    > well.
    >
    > Is this procedure possible/unusual/recommended?


    yes/yes/no.

    >
    > How do I do this from the command line ?


    You could add the dependant jar to the main jar using jar:

    jar -uf main.jar other.jar

    However, other.jar will not be visible to the class loader. For the class
    loader to be able to use it it would have to be extracted from main.jar
    first.

    It is actually simpler to package both jars into some other archiving
    container (zip, tar or even jar). Then unpack that to create the
    necessary structure.

    If, for example, the manifest of main.jar contains
    Class-Path: lib/other.jar

    then the contents of the archive would be
    main.jar
    lib/other.jar

    when extracted it should create the same disk structure.

    The manifest in main.jar will allow the class loader to find
    lib/other.jar. The path in the manifest is relative to the location of
    the executing jar, not the current directory.

    >
    > How do I tell Eclipse to not only reference the external *.jar lib but
    > ADD them physically into the target *.jar?
    >
    > As far as I know simply adding them on the Properties->Java Build
    > Path->Libraries dialog does NOT automatically add them into the target
    > *.jar but only references it.
    >


    I don't use Eclipse.

    --
    Nigel Wade
     
    Nigel Wade, Feb 3, 2010
    #2
    1. Advertising

  3. Ulf Meinhardt

    Lew Guest

    Re: Add *.jars to *.jars possible/recommendend? Eclipse setup forthis?

    Nigel Wade wrote:
    > Don't multi-post. I've just realized this has already been answered in
    > the other NG.


    !

    Ulf Meinhardt wrote:
    >> Assume I want to create a new program and to pack everything into a
    >> *.jar archive. To run the program one (or more) additonal external *.jar
    >> is required.
    >>
    >> Ok, the potential user could install the two *.jar files independently
    >> and add them to his CLASSPATH. This is somehow uncomfortable. I would
    >> like to deploy/provide simply one *.jar with everything needed inside.
    >>
    >> So I have to add the second, external jar into the "main" first jar as
    >> well.
    >>
    >> Is this procedure possible/unusual/recommended?


    Nigel Wade wrote:
    > yes/yes/no.


    Ulf Meinhardt wrote:
    >> How do I do this from the command line ?


    Nigel Wade wrote:
    > It is actually simpler to package both jars into some other archiving
    > container (zip, tar or even jar). Then unpack that to create the
    > necessary structure.
    >
    > If, for example, the manifest of main.jar contains
    > Class-Path: lib/other.jar
    >
    > then the contents of the archive would be
    > main.jar
    > lib/other.jar
    >
    > when extracted it should create the same disk structure.
    >
    > The manifest in main.jar will allow the class loader to find
    > lib/other.jar. The path in the manifest is relative to the location of
    > the executing jar, not the current directory.


    You can best do this with Ant.

    Ulf Meinhardt wrote:
    >> How do I tell Eclipse to not only reference the external *.jar lib but
    >> ADD them physically into the target *.jar?


    Don't. Do what Nigel said, or just pack the main and auxiliary JARs together
    in a directory and associated subdirectories that get copied to the
    application root directory.

    Words to the wise:

    JAR files are nearly complete vehicles for application delivery, combining
    features of ZIP with Java intelligence to know how to run the application.
    The "java" command has an option "-jar" that tells Java to run the program
    from the JAR file ("main.jar" in Nigel's example). Use of the "-jar" option
    suppresses all other classpath options and takes the classpath entirely from
    the (main) JAR manifest.

    The easiest, best, and most commonly used layout is what Nigel suggested, with
    the main JAR in the application directory and the library JARs on which it
    relies in the same directory or the "lib/" subdirectory thereof, as specified
    in the manifest. Do not use absolute paths or parent directory ("../") paths
    for the libraries.

    No more multiposting, now that you know.

    --
    Lew
     
    Lew, Feb 3, 2010
    #3
    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. Bill Grant
    Replies:
    1
    Views:
    465
    Ferenc Hechler
    Oct 20, 2004
  2. Ike
    Replies:
    6
    Views:
    1,084
    Andrew Thompson
    Sep 24, 2004
  3. Replies:
    5
    Views:
    9,093
  4. JavaEnquirer
    Replies:
    2
    Views:
    537
    JavaEnquirer
    Feb 22, 2006
  5. Sam Smith
    Replies:
    0
    Views:
    1,211
    Sam Smith
    Oct 18, 2004
Loading...

Share This Page