I have a property file inside a self-executing .jar. Actually the property file is in a jar inside the jar (packaged with one-jar). If I run my jar from the desktop it works fine, but when I put it inside the Program Files
directory it fails.
This is the code I'm using to load the property file:
public static void initialize(String language, String region) throws IOException {
try {
Locale locale = new Locale(language, region);
bundle = ResourceBundle.getBundle("resources.MessagesBundle", locale);
} catch (Exception e) {
InputStream is = Localization.class.getClassLoader()
.getResourceAsStream("resources/MessagesBundle_en_US.properties");
bundle = new PropertyResourceBundle(is);
}
}
I've done some experiments to try to get a better error message than "MissingResource", but so far it either works or doesn't work. Here is some more code that works unless it is in the Program Files directory:
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
InputStream is = contextClassLoader.getResourceAsStream(fileName);
if (is != null) {
int avail = is.available();
System.out.println("Available: " + avail);
} else {
System.out.println("Can't open!!!!!");
}
This is the full exception from ResourceBundle.getBundle
, the other functions just return null when a file can't be loaded:
Caused by: java.util.MissingResourceException: Can't find bundle for base name com.willwinder.universalgcodesender.i18n.MessagesBundle, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.willwinder.universalgcodesender.i18n.Localization.initialize(Localization.java:39)
at com.willwinder.universalgcodesender.i18n.Localization.getString(Localization.java:44)
at com.willwinder.universalgcodesender.SettingsFactory.(Settings
Factory.java:65)
... 7 more
So the question is: how can I load a resource bundle from a jar when the jar is executed in the Program Files
directory?
This is an open source project, the full source code is available here: https://github.com/winder/Universal-G-Code-Sender
The jar in question is here: https://github.com/winder/builds/blob/master/UniversalGCodeSender/UniversalGcodeSender-v1.0.7.zip?raw=true