How to get a resource in another jar

后端 未结 4 484
情书的邮戳
情书的邮戳 2021-01-14 14:24

I have a jar embedded in a bundle that needs to fetch a resource packaged with it like so:

MyBundle
  -\\ src
  -\\lib
    -\\MyEmbeddedJar
      -\\src
             


        
4条回答
  •  被撕碎了的回忆
    2021-01-14 14:57

    I Assume that SomeClass is inside the embedded jar (say, somejar.jar), and someResource.xml is in the outer jar, in a lib directory.

    In this case, there is no way to get to that in a non-OSGi context. Let's look at both situations in isolation.

    In OSGi

    Your someResource.xml should very well be reachable using the regular (non-OSGi specific) resource loading mechanisms, provided that it is reachable from the Bundle-ClassPath. For instance, if you have the following manifest header,

    Bundle-ClassPath: ., somejar.jar
    

    you will be able to get to your resource using "lib/someResource.xml". Notice the dot on the classpath: this means you can reach classes and resources from the root of the jar. If you forget that, you will only be able to get to classes and resources inside somejar.jar.

    Not using OSGi

    If you're not using OSGi, there is no (reasonably simple) way to get to classes and resources inside of the inner jar that I know of.

    Your options

    Depending on what you want your bundle to look like, you have two options now.

    1. Is it really necessary that SomeClass is in an embedded jar? If so, you're at a loss, and you jar will only work using OSGi.
    2. If you have the option to 'unpack' somejar.jar into your jar, you subvert the problem, and your jar can work in both situations.

    Personally, I'd pick option 2.: unless you have resources that might overwrite each other when you 'merge' the jars, it is no problem at all to have a slight mess of resources inside your bundle.

提交回复
热议问题