How to ensure loading of classes of external jars when called from an eclipse plugin?
I have developed an eclipse plugin which references an external jar present in a external installation directory. So I have added an entry to my bundle classpath as below:
Bundle-ClassPath: external:C:\mylib.jar
My class loads properly - and the plugin is able to detect a class MyClass
present in this external lib
.
a()
- I am calling in the class MyClass
is failing.
Method a() is as follows :
public void a()
{
URL url = this.class.getClassLoader().getResource("META-INF/startup-jar ");
...
}
so the URL which is returned is that of the eclipse plugin directory C:\eclipse3.4\test
and not of the physical location of the external jar which is C:\mylib.jar
This is causing method a()
to fail. Now, my question is -
how can I ensure the classloader gets the URL path of my external jar and not of my plugin directory?
Note : I cannot change the classloading mechanism in the external jar as it is a third party dependency and I have no control over the code. So please suggest a solution which would help me to load the external jar class correctly so I can get the correct URL.
Thanks a lot for your help - in advance
To explain a bit more on the problem I am facing :: My external jar is present开发者_JAVA百科 inside the installation directory of my server installation.
When the class in my external jar calls the URL url = this.class.getClassLoader().getResource("startup-jar")
it returns the URL relative to the eclipse bundle path - Something like C:\eclipse3.4...
and this URL is used for getting the boot directory (installation directory of the server) .
So it should have returned a path which is relative to the server installation directory, but instead returns a path relative to the eclipse installation directory.
Because of this, I am not able to call any APIs on the server as the server installation directory which it tries to use is incorrect.
So I wanted to know what is the best way I can handle this, so that this method call returns the server installation dir and not eclipse bundle path.
Can't you wrap this 3rd party dependency with the correct OSGI metadata and install it as a plug-in/bundle? We did this for all 3rd-party dependencies, including problematic ones such as Hibernate and made them work.
If it's a popular open source library, you can probably find it with the OSGi metadata added at Spring's repository: www.springsource.com/repository/app
In general, I wouldn't recommend the pattern of referencing external JARs as you describe in your question.
精彩评论