How to find the absolute path to properties loaded by resourceBoundle

I am loading properties using the code below:

ResourceBoundle boundle = ResourceBundle.getBundle("file") 

I want to know the absolute path of the downloaded file. For example, if I execute this code in a web application in the webapps tomcat folder, I want to get:

 c:\tomcat8\webapps\example\WEB-INF\classes\file.properties. 

How to find out this way?

+5
source share
2 answers

I decided to "solve" my problem with this "Get current working directory" message. For my problem, I only need this if I know the working directory, I can find the absolute path

 public class Test { public static void main(String... args) throws Exception { URL location = Test.class.getProtectionDomain().getCodeSource().getLocation(); System.out.println(location.getFile()); }} 

I think that perhaps this solution does not cover all situations, but for me this is enough.

0
source

You cannot get the physical location from the ResourceBundle , but if you know the path that was used to download it, you can find out its physical location. Like this:

 String resourceToLoad = "file"; URL url = this.getClassLoader().getResource(resourceToLoad); ResourceBoundle bundle = ResourceBundle.getBundle(resourceToLoad); 

url will have the physical location of the resource - at least most of the time.

Remember that a ResourceBundle can also be supported by the Java class, rather than something like a property file. ClassLoader.getResource does not understand the correspondence between ResourceBundle and Java classes. If you want to support them, you will have to implement a similar algorithm for what the ResourceBundle does to look for classes that fit this pattern.

0
source

Source: https://habr.com/ru/post/1243041/


All Articles