How to work with environment variables?

4

I'm developing an application that works with files (save and load) and I have to work with system variables to determine where to save those files.

My question is: Is there any way to use variables regardless of the operating system? If so, how should I proceed? For example, I know if I put a caminho variable, for example in windows as follows:

%caminho%\algum_dir\algum_arquivo.exemplo

It will convert %caminho% to the string contained in this variable. The same goes for Linux, only using $caminho . So, in short, I wanted to know if it is possible to use some kind of naming for the program to detect an environment variable, which has a name defined by me, independent of the OS, since the program will be in Java.     

asked by anonymous 16.03.2015 / 14:19

1 answer

3

I think what you want is the method getenv() . It is responsible for reading the environment variables and takes care of the specifics of the operating systems. I removed this code from the official tutorial :

import java.util.Map;

class Ideone {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
    }
}

See running on ideone .

I imagine you can get what you want from there. Documentation for Map . Essentially the key is the variable and the value is the content of it.

    
16.03.2015 / 14:34