Retrieve value from PATH environment variable in Linux using Java

3

I did not find a good topic on this subject and decided to create this question for those who are experiencing this problem. When using System.getenv("PATH") , all values of this environment variable are not listed. Tested points:

  • Execute on the linux terminal the echo $PATH command correctly lists all configured paths.
  • Turning off the machine does not work
  • ProcessEnvironment.environment() returns the same thing as the first code

I'm using this code:

String[] cmd = ['echo', '$PATH']
Process p = Runtime.getRuntime().exec(cmd);

BufferedReader retorno = new BufferedReader(new InputStreamReader(p.getInputStream()))
retorno.readLines()

Theoretically, it should return the same value as in the terminal, but besides not identifying some programs like 'help', using echo $PATH return is an array of size 1 with the $ PATH value in string. p>

Someone with an interesting solution to this problem?

    
asked by anonymous 19.10.2018 / 19:21

1 answer

0

Try this way

System.out.println(System.getenv("PATH"));
    
19.10.2018 / 21:21