The Eclipse plug-in developed RCP to get the Plugin/bundle file resource in the jar package after generating the jar package--take freemarker as an example

Source: Internet
Author: User



In the Eclipse plug-in development, there is a scenario where you need to set up a resource folder in a plug-in project that contains files such as profiles and templates. The problem that this article describes is that the resource files in the resource folder cannot be accessed after the plug-in project is packaged and published.



Problem Recurrence:



There are template resource folders at the root of the plug-in project, which need to be accessed when the plug-in is running. In plug-in development, the "Run as-Eclipse application" method is used to test the plug-in, but it can be accessed normally, but after the jar package is generated, it cannot be located to these resource files (as shown, the URL object pointing to the resource file is empty).



The problem code (for the purpose of locating the Freemarker template file location in the loopback environment, in this case the template file path is project_home (plug-in engineering root)/RESOURCES/*.FTL):

cfg = new Configuration ();
URL url = Activator.getDefault (). GetBundle (). GetResource ("resources");
//log.info("resources url: "+ url); // Test code, where log is private static Logger log = LoggerFactory.getLogger (FreeMarkerUtil.class);
//log.info("resources path: "+ url.toString ());
String resourcesPath = FileLocator.toFileURL (url) .getPath ();
File resourcesDir = new File (resourcesPath);
cfg.setDirectoryForTemplateLoading (resourcesDir); // The method of locating resources by FreeMarker calls different APIs according to different application scenarios, see the FreeMarker manual for details
Template template = cfg.getTemplate (templateName, "UTF-8"); // templateName is relative to the resources path, such as templateName = "a.ftl", the path is Project_HOME / resources / a.ftl
Analysis of the cause of the problem:

       Eclipse API obtains the absolute path of these resources through the path of these resources relative to Plugin / Bundle. The Eclipse API obtains the resources located in the bundle file directory through the IBundle interface. That is to say, the resource path must be configured in the bundle class loading path to ensure that the bundle can locate and access these resources. For in-depth knowledge about concepts such as Eclipse API, Bundle, and plug-in resources, it is recommended to read: http://blog.csdn.net/soszou/article/details/8034482.



      Solution to the problem in this article:

      1) Resource folder in project development


          Plugin Classpath configuration


         You can access it normally when testing the plug-in in the "Run as-Eclipse Application" mode, but after packaging and publishing, the resource URL obtained at runtime


        2) Solution: Open the editor of MANIFEST.MF, in the "Runtime" editing page, configure the resource folder ("resources" in this article) into Classpath,


        The result is:


        After adding, you can find that the icon of the "resources" folder has changed,


       After packaging and publishing, the resource URL obtained at runtime


        3) Conclusion:

        You must configure the resource path in the class loading path of the bundle to ensure that the bundle can locate and access these resources. In fact, the mechanism of loading the image files in the icons directory through the plug-in is associated with the loading of other resource files. In the Eclipse API, you can obtain the image resources in the plug-in project as follows:

public static ImageDescriptor getImageDescriptor (String path) {
return imageDescriptorFromPlugin (PLUGIN_ID, path);
}
        or

Image image = Activator.getImageDescriptor ("icons / workset.gif"). CreateImage ();
        This is because the plug-in project has configured the icons into the Classpath by default, which can be found in the "build.properties" page of the "MANIFEST.MF" file editor.


       After following the solution in this article, you can find that the resources folder has also been added to bin.includes,


End of the full text.

This article is from the "Bee" blog, please be sure to keep this source http://7369533.blog.51cto.com/7359533/1566946

Eclipse plug-in development After the RCP generates the jar package, obtain the Plugin / Bundle file resources in the jar package-take FreeMarker as an example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.