Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Environment variables are often a crucial and essential part of configuring an application. They primarily permit us to store sensitive data, configuration settings, and other variables we can access from the code. Setting up environment variables in IntelliJ IDEA is an easy and straightforward process.

In this tutorial, we’ll look at how to set up environment variables in IntelliJ IDEA. We’ll also look at the importance and use of the environment variable in an application.

2. Why Use Environment Variables

Using environment variables, we can run an application in different environments (local, stage, prod, dev). It primarily helps us to configure the project to run in different environments without modifying the actual code. Hence, using the environment variables, we can handle other distributions, making it platform-independent.

Environment variables also provide security in storing sensitive information like API keys, passwords, and auth tokens. Storing this information directly in the code increases potential security threats and risks.

3. Setting up Environmental Variables

In IntelliJ, we can set the Environment variables differently to enhance the overall development work and to secure and provide a dynamic project configuration. Primarily, they’re used to store sensitive information such as API keys, database tokens, and access tokens.

3.1. Using Run Configurations

We can add an environment variable to a project by editing the run configuration of an application. To illustrate, let’s look at the step-by-step process of adding environment variables:

  • Open IntelliJ IDEA
  • Select a project
  • At the top, select Edit Configurations under the Current File
  • Edit the environment variables and click on the + button
  • Add the name and value of the environment variable

We can see the complete process of adding an environment variable to a project in IntelliJ below:

envVariable

In the above process, we added the environment variable testUser baeldung into a project in IntelliJ by making changes in the run configuration.

3.2. Using the workspace.xml

We can also add environment variables to a configuration file and link it to a project by editing the run configuration of an application.

To demonstrate, let’s look at the process of editing the configuration file for environment variables:

  • Open the IntelliJ IDEA IDE
  • Select a project
  • At the top right, select Edit Configurations under the Current File
  • Tick the configuration to “Store as a project file.”

The below GUI contains the process to create an environment configuration file:

envStore

The above steps help us to create a configuration file in which we can define environment variables. Furthermore, the workspace.xml contains all the configurations related to a project or an application. Additionally, it has all the required commands to run and execute the application. We can also add Environment variables to this file to make them available to the project.

Now, let’s look at the steps to add an environment variable to workspace.xml:

<option name="env">
    <map>
        <entry key="articleCategory" value="baeldung-java" />
    </map>
</option>

In the existing workspace.xml, we added the above configuration containing the environment variable articleCategory with the value baeldung-java.

4. Conclusion

This article explored various ways to set the environment variable for an application using IntelliJ. First, we explored the importance of the environment variable for a project. After that, we set up the environment variable in the application using GUI and XML configuration files.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW