Files in "src/main/resources" automatically get bundled into the jar file, meaning that they can be used at runtime no matter where they are run.
If you want to access files inside the JAR, you can use methods provided by Java:
// Get the resource "test.properties" inside the current package.
getClass().getResource("test.properties");
// Get the resource "test.properties" inside "src/main/resources".
getClass().getResource("/test.properties");
If you want an input stream, you can use getResourceAsStream
:
InputStream propertiesInput = getClass().getResourceAsStream("/test.properties");
You can combine this with a Properties
object to read the resource as a properties file:
InputStream propertiesInput = getClass().getResourceAsStream("/test.properties");
Properties testProperties = new Properties();
testProperties.load(propertiesInput);
propertiesInput.close();