1

I am trying to run the below sample code in java.

import java.util.Locale;  
import java.util.ResourceBundle;

public class InternationalizationDemo {  

  public static void main(String[] args) {        
    ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH);  
    System.out.println("Message in "+Locale.CANADA_FRENCH +":"+bundle.getString("greeting"));
  }
}

1.The above code executes properly when MessageBundle.properties is placed in the class path. But I want to execute the above code successfully by removing the MessageBundle.properties from the classpath and placing it in some other location. How can I do this? Thanks in Advance.

4
  • possible duplicate of : stackoverflow.com/questions/3598240/… Commented Aug 23, 2016 at 10:38
  • No I don't want to place the properties file in any of the packages.
    – vinayaka
    Commented Aug 23, 2016 at 10:43
  • I want it in any of the drives like c:\ or D:\ drive
    – vinayaka
    Commented Aug 23, 2016 at 10:44
  • @vinayaka So then just check answers. There are already two solutions.
    – Hrabosch
    Commented Aug 23, 2016 at 10:56

2 Answers 2

2

You could use PropertyResourceBundle and get the path of your properties file from a System property for example something like that:

String configPath = System.getProperty("config.path");
ResourceBundle bundle = new PropertyResourceBundle(new FileReader(configPath));

Then in your launch command you will need to add -Dconfig.path=/path/to/my/config.properties

1

You can load properties file externally by this:

// Path to your file, if you have it on local, use something like C:\\MyFolder or \home\usr\etc
File file = new File("YOUR_PATH");
URL[] url = {file.toURI().toURL()};
ResourceBundle rb = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH, new URLClassLoader(url));

Example is from here: https://coderanch.com/t/432762/java/java/absolute-path-bundle-file

You are able to use remote Properties file or file which is saved on local.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.