Monday 15 June 2015

java - How to load resource bundle from file, not from jar, same bundle base name -


Both file systems and jar are resource bundles with the same base name Try to load the resource bundle from the file system, Not with jars

  URLClassLoader urlLoader = new URLClassLoader (new java.net.URL [] {filePathURL}); Resourcebundle bundle = Resersbundle.getbundle ("my.bundle", locale, URL loader);  

The bundle is loaded from the jar, not the file system. Why? If I changed the bundle base name to my.foo.bundle (different from the name of the bundle in jar), then it worked. But the name of the bundle is similar to the file system and jar. Is there any way?

Thank you.

I believe this is because URLClassLoader loads resources.

In the case of URLClassLoader:
whenever a resource is requested, it asks the parent class loader to load it first if the resource is not available , He checks the hair class loader.


It is not certain that this is the best way, but potential solutions:

1 Use Constructor

  `Public URL class loader (URL [] URL, class load loader parent)  

Null for the father

Similarly:

  URLClassLoader urlLoader = new URLClassLoader (new java.net.URL [] {filePathURL}, empty ); Resourcebundle bundle = Resersbundle.getbundle ("my.bundle", locale, URL loader);  

2. Increase the URL class loader and override the getResource method to load with your class loader first:


  public URL getResource (string name) {URL url = findResource (name); // it will be loaded with your class loader (url == faucet) {if (parent! = Null) {url = parent.getResource (name); } And {url = getBootstrapResource (name); }} Return URL; }  

No comments:

Post a Comment