0

Want to add "PGPASSWORD" env variable from my java application.

I know using below way, we can add.

private void setPgPasswordEnv(String pwdValue) {

    ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");

    Map<String, String> envMap = pb.environment();

    envMap.put("PGPASSWORD", pwdValue);
} 

If we add using above, will it persist till my application runs?

Is there any best way to add env variable from java?

  • it is not clear what you are trying to accomplish: you want to make this enviroment variable to be accessible by what other process? – user1708042 Apr 09 '20 at 07:36

1 Answers1

1

If you do that code, it applies to the new process(es) running the CMD program, when you subsequently call pb.start().

It does not affect the running Java program, and it does not affect any other ProcessBuilder, since each ProcessBuilder has it's own copy of the environment variables to set for the new process(es).

As fully explained in the documentation, i.e. the javadoc of environment().

Andreas
  • 154,647
  • 11
  • 152
  • 247