Search code examples
javajarclasspathresourcebundle

How to get ResourceBundle files out of classpath?


I am using a ResourceBundle for different languages (en, de, fr, ...) and the Keys are listed in a .properties file. It works great, but only using the classpath in the function getBundle().

Now, if I build my project as an executable JAR file, I can't maintain those properties/dictionary files. How can I go on to get those files out of the JAR file and use them (probably with a relative path)?


Solution

  • Check this if it's helpful. The thread lists a way to achieve this using absolute path (not relative).

    Inlining the code from link:

    File file = new File("/languages");
    URL[] urls = {file.toURI().toURL()};
    ClassLoader loader = new URLClassLoader(urls);
    ResourceBundle bundle = ResourceBundle.getBundle("english", Locale.getDefault(), loader);
    

    Also from URLs:

        URL[] urls = new URL[1];
     
        // - startfolder on the server, where he can find the package-structure like this.BUNDLE_NAME (http://somedomain.com/jws-stuff/com/myexample/test/)
        urls[0] = new URL("http://somedomain.com/jws-stuff/"); 
        ClassLoader loader = new URLClassLoader(urls);
        RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, Locale.GERMAN, loader);