Access relative path from eclipse-plugin

こ雲淡風輕ζ 提交于 2020-01-14 13:49:08

问题


Does anyone know how to get a file with uri from a self-made Eclipse Plug-in?

Absolute paths would be no problem:

URI.createFileURI("C:/Users/hp/workspace(dke)/SMartGen/StarSchema.profile.uml");

But how do I access local resources relatively?

URI.createFileURI("jar:file:/%ECLIPSE_HOME%/plugins/SMartGen.jar!StarSchema.profile.uml");

doesn't work this way....

Happy for every answer.

lg martin


回答1:


Use the FileLocator.

Example:

URL iconUrl = FileLocator.find(Platform.getBundle("myBundle"), new Path("icons/someIcon.png"), null);

This will get the URL of a file "someIcon.png" that is located in the "icons" folder in the bundle "myBundle".




回答2:


For getting a resource out of eclipse, you can use org.osgi.framework.Bundle.getEntry(String). That returns a standard java.net.URL, which can also be used to get the InputStream for consumption. It has the advantage of not caring if your plugin is in directory form, jar form, or in your workspace.

Bundle bundle = FrameworkUtil.getBundle(MyClass.class);
URL url = bundle.getEntry("StarSchema.profile.uml");

URL has a handy toURI() method as well.



来源:https://stackoverflow.com/questions/6253886/access-relative-path-from-eclipse-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!