I had an existing jar, which I have converted to OSGI Bundle now.
Problem is that now (as OSGI Bundle), it can not load resources. I am using ClassLoader.getSystemResource()
method, it returns null
.
Used code:
java.net.URL jdbc = ClassLoader.getSystemResource("com/company/cfg/provider/JDBC.xml");
I do not have access to source code so please tell some way by changing configuration (manifest file or something else) to make it work.
Bundle.getResource() works but I can not change the source code.
The reason for that is that an OSGi bundle uses it's own ClassLoaders. So what you want to do is get a hold of the OSGi ClassLoader:
java.net.URL jdbc = getClass().getClassLoader()
.getResource("com/company/cfg/provider/JDBC.xml");
Or in the case the call happens in a static context with in the class MyClass
:
java.net.URL jdbc = MyClass.class.getClassLoader()
.getResource("com/company/cfg/provider/JDBC.xml");
The difference between calling the static ClassLoader
and getClass().getClassLoader()
, is that in the first case you get an instance of java.lang.ClassLoader
which cannot handle OSGi bundles properly, and the second call will get a ClassLoader
that was used to load the current class which is part of an OSGi bundle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.