Classpath including JAR within a JAR

前端 未结 13 1385
自闭症患者
自闭症患者 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 03:08

    You need to build a custom class-loader to do this or a third-party library that supports this. Your best bet is to extract the jar from the runtime and add them to the classpath (or have them already added to the classpath).

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

    You do NOT want to use those "explode JAR contents" solutions. They definitely make it harder to see stuff (since everything is exploded at the same level). Furthermore, there could be naming conflicts (should not happen if people use proper packages, but you cannot always control this).

    The feature that you want is one of the top 25 Sun RFEs: RFE 4648386, which Sun, in their infinite wisdom, has designated as being of low priority. We can only hope that Sun wakes up...

    In the meanwhile, the best solution that I have come across (which I wish that Sun would copy in the JDK) is to use the custom class loader JarClassLoader.

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

    Use the zipgroupfileset tag (uses same attributes as a fileset tag); it will unzip all files in the directory and add to your new archive file. More information: http://ant.apache.org/manual/Tasks/zip.html

    This is a very useful way to get around the jar-in-a-jar problem -- I know because I have googled this exact StackOverflow question while trying to figure out what to do. If you want to package a jar or a folder of jars into your one built jar with Ant, then forget about all this classpath or third-party plugin stuff, all you gotta do is this (in Ant):

    <jar destfile="your.jar" basedir="java/dir">
      ...
      <zipgroupfileset dir="dir/of/jars" />
    </jar>
    
    0 讨论(0)
  • 2020-11-22 03:11

    If you have eclpise IDE, you just need to export your JAR and choose "Package Required libraries into generated JAR". eclipse will automatically add the required dependant JARs into the generated JAR as well as generated some eclipse custom class loader that load these JARs automatically.

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

    Well, there is a very easy way if you're using Eclipse.

    Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated Jar." Keep in mind, select "Extract..." and not "Package required libraries...".

    Additionally: You must select a run-configuration in your export settings. So, you could always create an empty main( ) in some class and use it for your run configuration.

    Anyway, it isn't guaranteed to work 100% of the time - as you will notice a pop-up message telling you to make sure you check the licenses of the Jar files you're including and something about not copying signature files. However, I have been doing this for years and have never encountered a problem.

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

    After some research I have found method that doesn't require maven or any 3rd party extension/program.

    You can use "Class-Path" in your manifest file.

    For example:

    Create manifest file MANIFEST.MF

    Manifest-Version: 1.0
    Created-By: Bundle
    Class-Path: ./custom_lib.jar
    Main-Class: YourMainClass
    

    Compile all your classes and run jar cfm Testing.jar MANIFEST.MF *.class custom_lib.jar

    c stands for create archive f indicates that you want to specify file v is for verbose input m means that we will pass custom manifest file

    Be sure that you included lib in jar package. You should be able to run jar in the normal way.

    based on: http://www.ibm.com/developerworks/library/j-5things6/

    all other information you need about the class-path do you find here

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