0

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

Winder
  • 1,984
  • 5
  • 23
  • 33
  • Show us the full error message. – Hot Licks Mar 30 '14 at 17:57
  • It looks like space symbol in path name. Have you tried to test this? You can also get look into code source, because you've got it. Everything is in place, it is easy. – Max Mar 30 '14 at 18:32
  • @Max Yes this is my project, I added localization and came across this problem. It looks like the problem is related to `one-jar`, the packaging tool I'm using. – Winder Mar 30 '14 at 18:38

1 Answers1

1

This is somehow related to one-jar, which is used to package native files along with jar files into a self-executable jar. These resource files originally looked like this:

basejar.jar
 \- lib\ (jar dependencies)
 |- binlib\ (native dependencies, serial lib, opengl, etc)
 |- main\
    \- main.jar (my code)
        \- resources\
            \- MessagesBundle_en_US.properties

After reorganizing the jar's such that the resources directory was at the top level in the container jar I'm now able to execute the jar from a protected directory:

basejar.jar
 \- lib\ (jar dependencies)
 |- binlib\ (native dependencies, serial lib, opengl, etc)
 |- main\
 |   \- main.jar (my code)
 |- resources\
     \- MessagesBundle_en_US.properties

I'm still not sure what the root cause is, but it seems to be a bug in one-jar.

Winder
  • 1,984
  • 5
  • 23
  • 33