Skip to main content

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 as MyConfig.cfg or sub/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) returns false (it returns true only 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(...) and update(...) replace the cached contents, so a Config built on this store can be modified and commit()-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​

StoreSourcePersisted writes
FileStoreFile system directoryYes
MemoryStoreIn memoryNo
ClasspathStoreJVM classpathNo

See Config Stores for the store contract.