Sling Academy
Home/Kotlin/How to Set Environment Variables in Kotlin Programs

How to Set Environment Variables in Kotlin Programs

Last updated: November 30, 2024

Environment variables are an essential part of any application configuration. They allow you to manage application settings outside the code, making it more versatile and secure. In this article, we'll go through how you can set and access environment variables in Kotlin programs.

Accessing Environment Variables

Kotlin, running on the JVM, provides you with the ability to interact with environment variables that are available to your program. You can access them using the System.getenv() method. Here’s an example:

fun main() {
    val path = System.getenv("PATH")
    println("The system PATH is: $path")
}

In this code snippet, we access the PATH environment variable and print it.

Setting Environment Variables for Local Development

While developing, you may need to set environment variables. There are several ways to do this, depending on your operating system and development environment.

On Windows

To set an environment variable on Windows:

  1. Go to the Start menu and search for 'Environment Variables'.
  2. Click on 'Edit the system environment variables'.
  3. In the System Properties window, click the 'Environment Variables...' button.
  4. Press 'New...' to create a new environment variable.
  5. Enter the name and value for your variable and press OK.

On macOS/Linux

You can set environment variables temporarily in your shell with the export command:

export MY_VAR="some_value"

To make them permanent, add the export statement to your shell configuration file, such as ~/.bashrc or ~/.zshrc:

echo 'export MY_VAR="some_value"' >> ~/.bashrc

After doing this, reload the file with:

source ~/.bashrc

Setting Environment Variables in Code (Not Recommended)

While it's generally not recommended due to security reasons, you can set environment variables directly in your Kotlin program if you want to. This can be achieved with Java interoperation, using System.setProperty() instead:

fun main() {
    System.setProperty("MY_APP_SETTING", "value")
    val mySetting = System.getProperty("MY_APP_SETTING")
    println("My App Setting: $mySetting")
}

This approach is more suited for application initialization rather than ongoing configuration since the properties set this way will only exist during the runtime of the application and will not modify system-wide environment variables.

Using .env Files (Recommendation)

.env files are a common way to manage environment variables in development. They take advantage of build tools or libraries to load the variables automatically. In a Kotlin project, you can use a library like java-dotenv:

dependencies {
    implementation("io.github.cdimascio:java-dotenv:5.2.2")
}

Create an .env file in your project directory:

MY_SECRET=supersecretvalue

Then, load and access it in your Kotlin program:

import io.github.cdimascio.dotenv.dotenv

fun main() {
    val dotenv = dotenv()
    val secret = dotenv["MY_SECRET"]
    println("The secret is: $secret")
}

Using .env files makes it easy to switch between different configurations for different environments (development, testing, production, etc.) and ensures that sensitive data isn’t hardcoded into the source files.

Next Article: Using Kotlin to Detect the Current Operating System

Previous Article: Reading System Properties with Kotlin

Series: Kotlin - File & OS

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin