Classpath including JAR within a JAR

前端 未结 13 1384
自闭症患者
自闭症患者 2020-11-22 02:21

Is it possible to specify a Java classpath that includes a JAR file contained within another JAR file?

相关标签:
13条回答
  • 2020-11-22 02:49

    If you're trying to create a single jar that contains your application and its required libraries, there are two ways (that I know of) to do that. The first is One-Jar, which uses a special classloader to allow the nesting of jars. The second is UberJar, (or Shade), which explodes the included libraries and puts all the classes in the top-level jar.

    I should also mention that UberJar and Shade are plugins for Maven1 and Maven2 respectively. As mentioned below, you can also use the assembly plugin (which in reality is much more powerful, but much harder to properly configure).

    0 讨论(0)
  • 2020-11-22 02:49

    Not without writing your own class loader. You can add jars to the jar's classpath, but they must be co-located, not contained in the main jar.

    0 讨论(0)
  • 2020-11-22 02:52

    I use maven for my java builds which has a plugin called the maven assembly plugin.

    It does what your asking, but like some of the other suggestions describe - essentially exploding all the dependent jars and recombining them into a single jar

    0 讨论(0)
  • 2020-11-22 02:54

    Winstone is pretty good http://blog.jayway.com/2008/11/28/executable-war-with-winstone-maven-plugin/. But not for complex sites. And that's a shame because all it takes is to include the plugin.

    0 讨论(0)
  • 2020-11-22 02:58

    I was about to advise to extract all the files at the same level, then to make a jar out of the result, since the package system should keep them neatly separated. That would be the manual way, I suppose the tools indicated by Steve will do that nicely.

    0 讨论(0)
  • 2020-11-22 03:04

    If you are building with ant (I am using ant from eclipse), you can just add the extra jar files by saying to ant to add them... Not necessarily the best method if you have a project maintained by multiple people but it works for one person project and is easy.

    for example my target that was building the .jar file was:

    <jar destfile="${plugin.jar}" basedir="${plugin.build.dir}">
        <manifest>
            <attribute name="Author" value="ntg"/>
            ................................
            <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
        </manifest>
    </jar>
    

    I just added one line to make it:

    <jar ....">
        <zipgroupfileset dir="${external-lib-dir}" includes="*.jar"/>
        <manifest>
            ................................
        </manifest>
    </jar>
    

    where

    <property name="external-lib-dir" 
              value="C:\...\eclipseWorkspace\Filter\external\...\lib" />
    

    was the dir with the external jars. And that's it...

    0 讨论(0)
提交回复
热议问题