Changeset 5241 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2012-05-18T12:45:10+02:00 (12 years ago)
Author:
bastiK
Message:

fixed #7703 - How to get resources from inside plugin?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/Plugin.java

    r3530 r5241  
    77import java.io.IOException;
    88import java.io.InputStream;
     9import java.net.URL;
     10import java.net.URLClassLoader;
    911import java.util.List;
    1012
     
    117119        out.close();
    118120    }
     121
     122    /**
     123     * Get a class loader for loading resources from the plugin jar.
     124     *
     125     * This can be used to avoid getting a file from another plugin that
     126     * happens to have a file with the same file name and path.
     127     *
     128     * Usage: Instead of
     129     *   getClass().getResource("/resources/pluginProperties.properties");
     130     * write
     131     *   getPluginResourceClassLoader().getResource("resources/pluginProperties.properties");
     132     *
     133     * (Note the missing leading "/".)
     134     */
     135    public ClassLoader getPluginResourceClassLoader() {
     136        File pluginDir = Main.pref.getPluginsDirectory();
     137        File pluginJar = new File(pluginDir, info.name + ".jar");
     138        URL pluginJarUrl = PluginInformation.fileToURL(pluginJar);
     139        URLClassLoader pluginClassLoader = new URLClassLoader(new URL[] { pluginJarUrl } , Main.class.getClassLoader());
     140        return pluginClassLoader;
     141    }
    119142}
Note: See TracChangeset for help on using the changeset viewer.