0

How to export $ORACLE_HOME and $PATH in java program so oracle related commands will work?

If I execute ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 and export PATH=$PATH:$ORACLE_HOME/bin from terminal now if i run Linux command lsnrclt status it gives result.

How can I do this using Java program so that will be in path and i can get result of lsnrctl status command result?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Possible duplicate of [Java processbuilder and using environment variables](https://stackoverflow.com/questions/30574972/java-processbuilder-and-using-environment-variables) – Robin Green May 04 '18 at 19:20

1 Answers1

2

Java's ProcessBuilder provides a environment() method which will return the environment (Map<String, String>) of the process when started. The environment will be initialized with System.getenv(), but the map can be freely changed to suit your needs. So simply Map.put the additional environment variables you need into the Map returned by ProcessBuilder.environment().

Alternatively you can export the environment variables on the shell for your Java program, in that case ProcessBuilder will pass them on to the child processes automatically.

pschichtel
  • 759
  • 7
  • 18
  • Thank You pschichtel for your quick response. Can you please give me t code soi can test if any issue I will let you know. The program that will export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 and export PATH=$PATH:$ORACLE_HOME/bin – Kailas Kakade May 04 '18 at 16:43
  • Big Thank pschichtel – Kailas Kakade May 04 '18 at 16:43
  • @KailasKakade its iterally just `builder.environment().put("ORACLE_HOME", "/u0..............")` – pschichtel May 04 '18 at 19:01