开发者

How to ensure loading of classes of external jars when called from an eclipse plugin?

开发者 https://www.devze.com 2022-12-22 02:37 出处:网络
I have developed an eclipse plugin which references an external jar present in a external installation directory.

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.

However, the method 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 -

As I don't have the external jar copied to my plugin directory (it is only present on the plugin classpath)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消