How to load resource from an absolute path?

227 Views Asked by At

I am using ff4j like this:

FF4J ff4j = new FF4J("config.xml");

Internally, FF4J calls getClass().getClassLoader().getResourceAsStream("config.xml").

I want to be able to choose the config.xml location at deployment time (for example, /etc for linux).

How can I achieve that, without having to hardcode an absolute path? Is it possible to set the JVM / tomcat to look for the file in /etc for example? Or maybe there's another way to achieve what I want with FF4J?

1

There are 1 best solutions below

0
clunven On

As creator of FF4j I would add a couple of solutions.

Before 1.8.9

File configFile = new File("/tmp/ff4j.xml");
FileInputStream fis = new FileInputStream(configFile);
FF4j ff4j = new FF4j(fis);

Since 1.8.9

 File configFile = new File("/tmp/ff4j.xml");
 FileInputStream fis = new FileInputStream(configFile);
 FF4j ff4j= new FF4j(new XmlParser(), fis);

As such you can now also import Yaml or properties files. (thinking about configMap in K8s world here). More samples here