0

I made a maven library jar which detect date formats based on configured formats mapping. Currently I hard-coded the formats mapping but I want to isolate the formats mapping into a .properties files as below:

  • Internal .properties file (has some default formats mapping).
  • External .properties file (dateformats.properties, a file that client may add it to his application CLASSPATH to extend the default formats mapping provided by the library).

Reading the internal .properties file from library CLASSPATH is easy by but how to let the library read the external .properties file from client CLASSPATH?

Ayman Al-Absi
  • 2,630
  • 24
  • 22
  • 1
    There is no distinction between the "library CLASSPATH" and the "client CLASSPATH", Java works with only one classpath, which is a collection of directories and JAR files in which the JVM looks for `*.class` files and other resources. – Jesper Nov 12 '14 at 21:37

1 Answers1

2

Number of options are available to you:

  • have the client specify the Properties instance that you'll use (so your API would recive the java.util.Properties typed as one of it's arguments)
  • search the classpath for other .properties files and prefer the client's one (get the java.class.path property and search the jar entries or be inventive with getResources method)
  • use the client's classloader (one method of doing this is examining the stack and determining he client's class and then access it's classloader, another would be to have the client pass it as a parameter)

Each method has its pluses and minuses and it depends upon your needs...

Community
  • 1
  • 1
Zoran Regvart
  • 4,630
  • 22
  • 35
  • Thank you for answering, For first option can you make it more clear. Do you mean to ask the client to provide file URI in constructor of the class? – Ayman Al-Absi Nov 13 '14 at 06:51
  • Hi, hope this makes it clearer, I've added that you receive Properties instance, but you can also make it that your API receives URI instead of that – Zoran Regvart Nov 13 '14 at 07:25