如何在另一个jar中获取资源 [英] How to get a resource in another jar

查看:160
本文介绍了如何在另一个jar中获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入在一个包中的jar,需要获取与它一起打包的资源,如下所示:

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

MyBundle
  -\ src
  -\lib
    -\MyEmbeddedJar
      -\src
        -\SomeClass
      -\someResource.xml

我试图从'SomeClass'访问'someResource.xml',如下所示:

I am trying to access 'someResource.xml' from 'SomeClass' like so:

SomeClass.class.getResource( "someResource.xml" ); 

但我没有运气。我已经尝试了附加CWD的几种变体(例如:'。/ someResource.xml'),但我无法加载此资源。

But I've had no luck. I've tried several variations with the CWD appended (eg: './someResource.xml') but I just can't get this resource to load.

我知道正确的方法是使用Activator来获取钩子回到正确的类加载器,但是嵌入式jar可以用在其他项目中,所以我不想在它上面添加OSGi特定代码才能使它发挥作用OSGi很好。

I know that the "right" way is to use Activator to get hooks back to the proper classloader, but the embedded jar can be used in other projects, so I'd hate to have to add OSGi specific code to it just to get it to play nice with OSGi.

还有其他方法可以在OSGi的OSGi中加载资源吗?

Is there any other way to load resources in OSGi agnostically of OSGi?

推荐答案

我假设 SomeClass 在嵌入式jar中(比如, somejar.jar )和 someResource.xml 位于外部jar中,位于 lib 目录中。

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

在这种情况下,无法在非OSGi上下文中找到它。让我们一起看看这两种情况。

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

你的 someResource.xml 应该可以使用常规(非特定于OSGi)的资源加载机制,提供可以从 Bundle-ClassPath 。例如,如果您有以下清单标题,

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

您可以使用lib / someResource.xml
注意类路径上的点:这意味着您可以从jar的根目录获取类和资源。如果您忘记了,您将只能访问 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.

如果您没有使用OSGi,则没有(相当简单的)方法可以获得我所知道的内部jar中的类和资源。

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.

根据您希望捆绑包的外观,您现在有两种选择。

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


  1. SomeClass 是否真的有必要在嵌入式jar中?如果是这样,你就会感到茫然,而你的jar只会使用OSGi。

  2. 如果你可以选择'解包' somejar.jar 进入你的jar,你破坏了问题,你的jar可以在两种情况下都有效。

  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.

我个人而言d选择选项2:除非你有资源在你'合并'罐子时可能会互相覆盖,所以在你的包里面有一点点资源是没有问题的。

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.

这篇关于如何在另一个jar中获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆