Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this program throwing java.lang.UnsupportedOperationException

Tags:

java

import java.util.Map;

class Test {
    public static void main(String[] args) {
        Map<String, String> env = System.getenv();
        System.out.println(env);
        env.put("ID", "1");
    }
}

I am adding some key value pairs in the map while initializing and then again adding one key value pair so what is wrong with the program?

like image 655
Abhishek Honey Avatar asked Feb 20 '26 17:02

Abhishek Honey


1 Answers

No you cannot modify the returned map.

System#getEnv() returns unmodifiable map

Returns an unmodifiable string map view of the current system environment.

The reason being is that the map contains info about system that being run and you cannot simply add info programatically.

like image 108
Suresh Atta Avatar answered Feb 22 '26 05:02

Suresh Atta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!