A 12-factor Java API configuration library
This library is written for Java 8 or great
With maven, you should compile with:
mvn clean install
After compiled, you can add it in your pom.xml
these lines:
<dependency>
<groupId>org.library</groupId>
<artifactId>envfile</artifactId>
<version>0.1.2</version>
</dependency>
Suppose you have an .env
file in your project like this:
VAR1="this is a value"
VAR2=123
you can load it with this code:
import org.library.envfile.EnvFile;
public class Example {
public static void main(String[] args) {
EnvFile.load();
System.out.println(System.getenv("VAR1"));
System.out.println(System.getenv("VAR2"));
}
}
Another usage is to specify entire path of the env file:
import org.library.envfile.EnvFile;
public class Example2 {
public static void main(String[] args) {
EnvFile.load("/path/for/.env");
System.out.println(System.getenv("VAR1"));
System.out.println(System.getenv("VAR2"));
}
}
This library is written with help of this issue of StackOverflow. This means that when you use this library, you might receive this warning:
WARNING: An illegal reflective access operation has occurred
To suppress this warning, use --illegal-access=warn
as JVM option. Sadly, at the moment, I cannot know another method to
avoid it