ClasspathStore
The ClasspathStore reads configuration files
from the JVM classpath (for example files packaged inside your jar or under src/main/resources).
Example
// Read "MyConfig.cfg" from the classpath.
Config config = Config
.create("MyConfig.cfg")
.store(ClasspathStore.DEFAULT)
.build();
String name = config.getString("General/name");
A shared instance is available as ClasspathStore.DEFAULT, or you can create a new one with ClasspathStore.create().build().
The class is thread safe and reusable.
Behavior
- Lookup.
read(String)loads the named resource through the current thread's context class loader (Thread.currentThread().getContextClassLoader().getResourceAsStream(name)). The name is used as-is, so it is a classpath resource path such asMyConfig.cfgorsub/foo2.cfg; no file extension search is performed (unlike FileStore). - Missing files. A resource that does not exist reads as an empty string, and
exists(String)returnsfalse(it returnstrueonly when the resource has non-empty contents). - Caching. Contents are cached per name after the first read.
- Writes are in-memory only. Classpath resources can't be written back.
write(...)andupdate(...)replace the cached contents, so aConfigbuilt on this store can be modified andcommit()-ed and will see its own changes, but nothing is persisted and the change is lost when the JVM exits. Writing to a name that does not exist on the classpath creates an in-memory entry. - No watcher. There is no file watching; the store does not detect changes to the classpath resource.
- close(). No-op.
Builder
ClasspathStore.Builder exposes no settings beyond those inherited from ConfigStore.Builder.
System default config
ClasspathStore.DEFAULT is also what the system default config lookup falls back to:
when Config.getSystemDefault() is searching for a config, it first tries the file system via FileStore.DEFAULT
and, if the file is not found there, tries the classpath via ClasspathStore.DEFAULT.
See System Default Config.
Choosing a store
| Store | Source | Persisted writes |
|---|---|---|
| FileStore | File system directory | Yes |
MemoryStore | In memory | No |
ClasspathStore | JVM classpath | No |
See Config Stores for the store contract.