Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4738465

(process) Need to create subprocess environment derived from current environment

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 1.3.0, 1.3.1
    • Fix Version/s: 5.0
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      tiger
    • CPU:
      generic
    • OS:
      generic

      Description



      Name: pa48320 Date: 08/28/2002


      When we develop new feature of environment switching for Oracle Reports product, we look into jre's Runtime.exec(cmdline, envp) function to spawn a new process with a set of environment. We found once we put environment to envp, some functionality (such as orb.init()) in the sub-process stop working. We searched sun's bugdb and found related bug 4115037 and 4064912, and understood the reason of the failure. Although there is a workaround for the problem, but we think the workaround is against 100% pure java implementation for main process, because in java there is no way to get environment variable. We have to call jni into native function to get certain environment variables and add them to envp.

      We (as well as from many comments in the bugdb) think it is highly desirable feature to have another flavor of Runtime.exec() which inherits the environment from main process plus new
      environment specified by envp.

      Following is the source code to demostrates the problem. Run it on Windows platform. If you run
      java MainProc CreateOrb
      it will succeed. If you run
      java MainProc CreateOrb -e
      it will fail because it clears the several important Windows environment variables.

      1. MainProc.java - Main process:
      import java.io.*;

      public class MainProc
      {
        static Process p;

        public static void main(String []args) throws IOException,
                                                      InterruptedException
        {
          if (args.length == 0)
          {
            System.out.println("Usage: java MainProc <SubProcessClassName> [-e]");
            System.out.println(" <Default> Spawns SubProc without environment");
            System.out.println(" -e Spawns SubProc with environment");
            return;
          }

          Runtime rt = Runtime.getRuntime();

          String cmdLine = "javaw -cp . " + args[0];

          /*
           * Spawn the process with environment setting
           */
          if (args.length >= 2 && args[1].equalsIgnoreCase("-e"))
          {
            String[] envp = new String[1];
            envp[0] = "Foo=Bar";

            p = rt.exec(cmdLine, envp);
          }
          /*
           * Spawn the process without environment setting
           */
          else
          {
            p = rt.exec(cmdLine);
          }

          /*
           * Catch the process in action
           */
          StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
          StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
          errorGobbler.start();
          outputGobbler.start();

          int rv = p.waitFor();

          System.out.println("Return value: "+rv);
        }

      }

      class StreamGobbler extends Thread
      {
        InputStream is;
        String type;

        StreamGobbler(InputStream is, String type)
        {
            this.is = is;
            this.type = type;
        }

        public void run()
        {
          try
          {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ((line = br.readLine()) != null)
              System.out.println(type+">"+line);
          }
          catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
        }
      }

      2. CreateOrb.java - Sub process:
      public class CreateOrb
      {
        public static void main(String []args)
        {
          try
          {
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

            if (orb != null)
              System.out.println("Successfully created an ORB object");
            else
              System.out.println("orb is null");
            System.exit(0);
          }
          catch (Exception e)
          {
            e.printStackTrace();
            System.exit(-1);
          }
        }
      }
      (Review ID: 163687)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                martin Martin Buchholz
                Reporter:
                pallenba Peter Allenbach (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: