Search code examples
batch-fileenvironment-variablesoc4j

Batch script for set env. variable startup OC4J and remove env. variable


on local computer are installed an Oracle client (11.2.0) and an OC4J Server (Oracle Containers for J2EE 10g (10.1.3.5.0) (build 090727.2000.36696)), both of them are using the ORACLE_HOME enviroment variable so I need to set ORACLE_HOME pointing to server folder only when server starts

I'm trying to generate batch file that must do:

  1. Set enviroment variable ORACLE_HOME
  2. Start up OC4J server
  3. Unset ORACLE_HOME variable

I'm trying with this script but the third statement never runs.

call setx -m ORACLE_HOME "C:\Servers\oc4j_extended_101350" 
call C:\Servers\oc4j_extended_101350\bin\oc4j -start 
call REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V ORACLE_HOME

All of this commands works fine executing individually. But, on the same batch script the Start up OC4J "never" ends. Any idea how can i do this works?

Any help would be appreciated


Solution

  • The batch file to start the Oracle server just needs the following 2 lines:

    set "ORACLE_HOME=C:\Servers\oc4j_extended_101350"
    C:\Servers\oc4j_extended_101350\bin\oc4j.exe -start
    

    That's it if oc4j.exe is not a console application and therefore command processor immediately continues processing the batch file after starting oc4j.exe resulting in closing command process.

    Otherwise use:

    set "ORACLE_HOME=C:\Servers\oc4j_extended_101350"
    start "Oracle Server" C:\Servers\oc4j_extended_101350\bin\oc4j.exe -start
    

    Why this works?

    Windows creates automatically a copy of the entire environment table of current process for the new process on creating a new process.

    For the command process executing the batch file ORACLE_HOME is set in its environment table as specified in the batch file.

    On starting the Oracle server this environment table is copied by Windows for the Oracle server including ORACLE_HOME as currently defined. What is defined in Windows registry does not matter and is not taken into account. The Oracle server does not see if there is also ORACLE_HOME set at all and if so with which value for parent processes or other processes running parallel.

    A simple example to demonstrate environment table management by Windows.

    1. Open a command prompt window and enter set x=Hello.
    2. Type set x and you see x=Hello.
    3. Execute start resulting in opening a second command prompt window.
    4. Type in this second command prompt window set x and you get displayed also x=Hello.
    5. Switch back to first command prompt window and run set x=Hi.
    6. Type in this first command prompt window set x and you see x=Hi.
    7. Switch again to second command window, type set x and you still see set x=Hello.

      This second command process has got a copy of first command process. So what is changed now in the environment table of first command process is not visible for second command process.

    8. Execute in second command window set x=Bye and verify it with set x.

    9. Switch back to first command window and enter set x.

      It is still output x=Hi because also the parent process does not get back what the child process modifies in its copy of the environment table.

    10. Switch to second command window and enter set path= to delete environment variable PATH from environment table of this process.

    11. Execute once more start to open from second command window a third command window.
    12. Enter set path and you get displayed just

      PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
      

    What happened with system PATH?

    System PATH as well as user account related PATH are still set in Windows registry and build together PATH for new processes started from desktop Explorer process. But in the environment tables of second and third command process there is no environment variable PATH anymore. Those two processes must work now without environment variable PATH. Of course for the first command process and all other running processes PATH still exists in their environment tables.