Spring app doesn't read environmental variables from .env

I have .env file in my project root folder along with pom.xml. Contents:
APP_PASSWORD=my-app-password
DATABASE_PASSWORD=Tomasm21-xi
DATABASE_USERNAME=Tomasm21
APP_PASSWORD=my-app-password
DATABASE_PASSWORD=Tomasm21-xi
DATABASE_USERNAME=Tomasm21
and in application properties I use these values:
# Database Configuration
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myDB
spring.datasource.username=${DATABASE_USERNAME}
spring.datasource.password=${DATABASE_PASSWORD}
//....
spring.mail.username=${MAIL_USERNAME}
spring.mail.password=${APP_PASSWORD}
# Database Configuration
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myDB
spring.datasource.username=${DATABASE_USERNAME}
spring.datasource.password=${DATABASE_PASSWORD}
//....
spring.mail.username=${MAIL_USERNAME}
spring.mail.password=${APP_PASSWORD}
I even added Dotenv Dependency:
<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.2.2</version>
</dependency>
and configured it:
import io.github.cdimascio.dotenv.Dotenv;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DotenvConfig {
@Bean
public Dotenv dotenv() {
return Dotenv.configure().ignoreIfMissing().load();
}
}
import io.github.cdimascio.dotenv.Dotenv;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DotenvConfig {
@Bean
public Dotenv dotenv() {
return Dotenv.configure().ignoreIfMissing().load();
}
}
This class should set the system properties using the values from the Dotenv bean:
@Configuration
public class AppConfig {
private final Dotenv dotenv;

@Autowired
public AppConfig(Dotenv dotenv) {
this.dotenv = dotenv;
System.setProperty("DATABASE_USERNAME", dotenv.get("DATABASE_USERNAME"));
System.setProperty("DATABASE_PASSWORD", dotenv.get("DATABASE_PASSWORD"));
System.setProperty("MAIL_USERNAME", dotenv.get("MAIL_USERNAME"));
System.setProperty("APP_PASSWORD", dotenv.get("APP_PASSWORD"));
}
}
@Configuration
public class AppConfig {
private final Dotenv dotenv;

@Autowired
public AppConfig(Dotenv dotenv) {
this.dotenv = dotenv;
System.setProperty("DATABASE_USERNAME", dotenv.get("DATABASE_USERNAME"));
System.setProperty("DATABASE_PASSWORD", dotenv.get("DATABASE_PASSWORD"));
System.setProperty("MAIL_USERNAME", dotenv.get("MAIL_USERNAME"));
System.setProperty("APP_PASSWORD", dotenv.get("APP_PASSWORD"));
}
}
But actually it doesn't work and I get an exception when I launch my application. I'm on Windows 10.
9 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Tomasm21! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Tomasm21
Tomasm21OP5mo ago
The exception tells that my database is inaccessible:
Tomasm21
Tomasm21OP5mo ago
One of ways to succesfully launch the app is to set environment variables through Eclipse Run Configurations... in its Environment tab:
No description
Tomasm21
Tomasm21OP5mo ago
But it works only if I launch on Eclipse. But the app fails if I launch it through CLI: $mvn spring-boot:run ---- How to enable Spring Boot app to read my .env and succesfully launch? It can be launched as mvn spring-boot:run -Dspring-boot.run.arguments="--spring.datasource.username=Tomasm21,--spring.datasource.password=Tomasm21-xi,[email protected],--spring.mail.password=my-app-password" But it does not solve reading from .env problem.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm21OP5mo ago
well lets see
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Tomasm21
Tomasm21OP5mo ago
Unfortunately that article users answers does not solve reading environmental variables from .env file to the Environment specifics. Fortunately I have found how elsewhere. And when using Spring Boot project in docker container too.
JavaBot
JavaBot5mo ago
Post Closed
This post has been closed by <@312509109863710732>.
Want results from more Discord servers?
Add your server