Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
How to Read Environment Variables in Scala?
Next article icon

How to Read Environment Variables in Scala?

Last Updated : 08 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Scala stands for scalable language. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically typed language although unlike other statically typed languages like C, C++, or Java, it doesn't require type information while writing the code. The type verification is done at the compile time. Static typing allows to building of safe systems by default. Smart built-in checks and actionable error messages, combined with thread-safe data structures and collections, prevent many tricky bugs before the program first runs. In this article, we will see how we can read environment variables in Scala.

Table of Content

  • What are Environment Variables?
  • Reading Environment Variables in Scala
    • Using Scala's sys Object
    • Using Scala's Properties Object
    • Using Java's System Object
  • Conclusion

What are Environment Variables?

Environment variables are a special type of variable that is stored outside the program and can affect how the program runs depending on the computer. For example, the HOME environment variable represents the home directory.

Reading Environment Variables in Scala

There are several different ways in which we can read environment variables in Scala. We are going to see three different ways to achieve the same.

Using Scala's sys Object

We can use the sys object provided by Scala to interact with environment variables. The env method provided by sys returns an immutable map containing all the environment variables along with their values.

sys.env

Output:

res0: scala.collection.immutable.Map[String, String] = Map(PATH -> /usr/local/bin:/usr/sbin, HOME -> /usr/home)

sys.env
sys.env

As the env method returns an immutable map object, we can simply reference it to get the value of a specific environment variable.

sys.env("PWD")

Output:

res0: String = /path/to/present/directory

sys.env("PWD")
sys.env("PWD")

We can also make use of the get method of map object to get the value of a specific environment variable.

sys.env.get("PWD")

Output:

res0: Option[String] = Some(/path/to/present/directory)

sys.env.get("PWD")
sys.env.get("PWD")

The getOrElse method provided by map can also be used to get the value and return some default value if the environment variable doesn't exist.

sys.env.getOrElse("FOO", "undefined")

Output:

res0: String = undefined

sys.env.getOrElse("FOO", "undefined")
sys.env.getOrElse("FOO", "undefined")

Using Scala's Properties Object

Scala provides the developers with Properties object which, among other things, contains methods to read environment variables.
The envOrElse method provided can be used to read the value of an environment variable if it exists or return some pre-defined value if it doesn't exists.

scala.util.Properties.envOrElse("PWD", "/some/value")

Output:

res0: java.lang.String = /path/to/present/directory

scala.util.Properties.envOrElse("PWD", "/some/value")
scala.util.Properties.envOrElse("PWD", "/some/value")

Similar to the envOrElse method, the Properties object provides envOrNone method which returns None if the environment variable doesn't exist.

scala.util.Properties.envOrNone("FOO")

Output:

res0: Option[java.lang.String] = None

scala.util.Properties.envOrNone("FOO")
scala.util.Properties.envOrNone("FOO")

Using Java's System Object

As Scala is interoperable with Java, we can also make use of capabilities offered by Java. The System object provided by Java, provides methods for developers to interact with environment variables. Akin to sys.env method, System contains getenv method which returns a Map object containing all the environment variables along with its values.

java.lang.System.getenv()

Output:

res0: java.util.Map[java.lang.String, java.lang.String] ={PATH=/usr/local/bin:/usr/sbin, HOME=/usr/home}

java.lang.System.getenv()
java.lang.System.getenv()

As the getenv method returns a map object, we can simply reference it to read the value of an environment variable.

java.lang.System.getenv("PWD")

Output:

res0: java.lang.String = /path/to/present/directory

java.lang.System.getenv("PWD")
java.lang.System.getenv("PWD")

If the environment variable doesn't exist referencing it will return null

java.lang.System.getenv("FOO")

Output:

res0: java.lang.String = null

java.lang.System.getenv("FOO")
java.lang.System.getenv("FOO")

Conclusion

In this article, we saw how we can use Scala to read environment variables. We had a chance to go through several methods to achieve so. We first saw the capabilities provided by Scala, sys object and Properties object, and later saw capabilities provided by Java, System object, which we can make use in Scala owing to its interoperability with Java.


Next Article
How to Read Environment Variables in Scala?

A

asinhavef4
Improve
Article Tags :
  • Scala

Similar Reads

    How To Set Environment Variables In Jenkins ?
    Jenkins is a widely used open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) processes. One powerful feature of Jenkins is its capability to configure environment variables, which are important for various functions like defining parameters, paths, a
    6 min read
    How to add environment variable in MacOS?
    While programmers work on different Programming Languages, they use to declare Variables to work on any certain piece of code. The Variables in general terms are used to store some values that can be again accessed in the future. There are some Operating System Variables present that are known as En
    3 min read
    Environment Variables in Java
    In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
    5 min read
    How to Read and Write CSV File in Scala?
    Data processing and analysis in Scala mostly require dealing with CSV (Comma Separated Values) files. CSV is a simple file format used to store tabular data, such as a spreadsheet or database. Each line of a CSV file is plain text, representing a data row, with values separated by commas (,). Readin
    5 min read
    Read Environment Variables with Python dotenv
    Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible softwa
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences