2

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)?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
marc3l
  • 2,525
  • 7
  • 34
  • 62

1 Answers1

3

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);
            
 
tgkprog
  • 4,493
  • 4
  • 41
  • 70
John Jai
  • 3,463
  • 6
  • 25
  • 32
  • in case the link does not work in future: File file = new File("C:\\languages"); URL[] urls = {file.toURI().toURL()}; ClassLoader loader = new URLClassLoader(urls); ResourceBundle bundle = ResourceBundle.getBundle("english", Locale.getDefault(), loader); – tgkprog Aug 18 '21 at 17:04
  • Even a way to make it load from URL for regular apps (non jnlp): 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); – tgkprog Aug 18 '21 at 17:05