How to get a resource in another bank

I have a jar built into a package that should receive a resource packaged with it like this:

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

I am trying to access "someResource.xml" from "SomeClass" as follows:

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

But I’m out of luck. I tried several options with added CWD (for example: ./ someResource.xml), but I just can't load this resource.

I know that the “right” way is to use Activator to return to the appropriate classloader, but the built-in jar can be used in other projects, so I would not want to add special OSGi code to it to make it play well with OSGi.

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

+3
4

I , SomeClass jar (, somejar.jar), someResource.xml lib.

, OSGi. .

OSGi

someResource.xml , ( OSGi) , , Bundle-ClassPath. , ,

Bundle-ClassPath: ., somejar.jar

, "lib/someResource.xml". : , . , somejar.jar.

OSGi

OSGi, ( ) , .

, , , .

  • , SomeClass ? , , OSGi.
  • "" somejar.jar , , .

2. , , "" , .

+5

:

  • , , . *.jar?
  • , - , .
  • OSGi, , OSGi.

, InputStream, , MyBundleClass.class.getResourceAsStream("/pathToJar.jar");, java.util.jar.JarInputStream. getNextJarEntry(), JarEntry, "someResource.xml".equals(jarEntry.getName()).

+2

@Angelo, , , , - , .

, SomeClass, java.net.URL. someResource.xml .

SomeClass :

new SomeClass( FileLocator.find( Activator.getDefault().getBundle(), new Path( "./someResource.xml" ), new HashMap< String, String >() ) );

. , SomeClass? , , ?

0

:

embedded.jar
    src/SomeClass.class
    someResource.xml

embedded.jar embedded.jar Bundle-Classpath.

SomeClass.class.getResource("someResource.xml") , src/someResource.xml, SomeClass src. someResource.xml , SomeClass.class.getResource("/someResource.xml") .

OSGi, , Java.

0
source

Source: https://habr.com/ru/post/1781560/


All Articles