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

Runtime.exec() - envp[] not passed correctly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.1.1, 1.1.3
    • core-libs
    • x86
    • windows_95, windows_nt



      Name: sgC58550 Date: 06/25/97


      envp[] argument of exec() method is not passed correctly to an invoked process.

      [Java Source(ztProcess.java)]
      import java.io.*;
      class ztProcess {
      ztProcess(String strCmd) {
      String astrEnvVar[] ={
      "XXX=x",
      "YYY=y",
      "ZZZ=z"
      };
      try {
      Process proc = Runtime.getRuntime().exec(strCmd, astrEnvVar);
      } catch (IOException exp) {
      System.out.println(exp.getMessage());
      }
      }
      public static void main(String argv[]) {
      new ztProcess(argv[0]);
      }
      }

      [C Source(EnvEcho.c)]
      #include <stdio.h>
      int main(int argc, char *argv[], char *envp[])
      {
      FILE *fpo;
      int i;
      fpo = fopen("C:\\temp\\EnvEcho.txt", "w");
      for (i = 0; envp[i] != NULL; i++) {
      fprintf(fpo, "envp[%d]: %s\n", i, envp[i]);
      }
      fclose(fpo);
      return i;
      }

      [Sample Operation]
      >java ztProcess EnvEcho.exe

      [Result]
      Only the first environment variable('XXX=x') is passed, but not the others.

      company - Mitsui Knowledge Industry , email - ###@###.###
      ======================================================================


      Name: rlT66838 1997-08-06

      I have found a problem with the use of the "envp" parameter on the "exec"
      method of Runtime. When "envp" is used, there is no error, but the
      environment indicated by this parameter is not passed to the program which is
      being started.

      I have written some simple code which illustrates the problem. When the
      "wedge11" program is run, it starts "getvars" which displays certain
      environment variables.

      When "wedge11" is run using the code block that starts with the comment
      "Start of code using envp parameter", the environment is not passed.

      When "wedge11" is run using the code block that starts with the comment
      "Start of code using only a command line", the environment is passed.

      Only the "wedge11" program needs to be run, it receives the output from
      "getvars" and displays it.

      This problem has been tested on Windows 95 running JDK 1.1.3 and on
      Solaris 2.51 running JDK 1.1_Final with the same results.

      Here is the code for the two programs.

      --------------------------start of file "wedge11.java"---------------------
      import java.io.*;
      import java.util.*;

      public class wedge11 {

          public static void main(String[] args) {

              try {

                  //create an array of strings for the environment
                  String envp[] = new String[5];
                  int i = 0;
                  envp[i++] = new String("REQUEST_METHOD=POST");
                  envp[i++] = new String("CONTENT_LENGTH=50");
                  envp[i++] = new String("QUERY_STRING=ThisIsTheData");

                  Runtime r = Runtime.getRuntime();

      // -----Start of code using envp parameter. This does not work.----------
                  Process p = r.exec("java getvars", envp);
      // -----End of code using envp parameter----------

      /** comment block start **
      // -----Start of code using only a command line. This works correctly.----------
      //build a command line from "envp" using the -D switch
                  i = 0;
                  String command = new String("java ");
                  while(envp[i] != null) {
                      command = command.concat("-D");
                      command = command.concat(envp[i++]);
                      command = command.concat(" "); //seperator
                  }
                  command = command.concat("getvars"); //class name

                  //display the command
                  System.out.println("wedge11: command = " + command);

                  //start the target program with the command line
                  Process p = r.exec(command);
      // -----End of code using only a command line----------
      ** comment block termination **/

                  //build an input stream for the target program's standard output
                  BufferedReader targetIn = new BufferedReader(
                                  new InputStreamReader(p.getInputStream()));

                  //read from the target program's standard output, display it
                  String work;
                  while ((work = targetIn.readLine()) != null) {

                      //handle the special case of an empty line, which is a newline
                      if (work.length() == 0)
                          work = "\n";

                      //display result
                      System.out.println("redirected output:" + work);
                  }

              }
              catch (IOException e) {
                  System.err.println("wedge11: IOException:" + e);
              }
          }
      }
      --------------------------end of file "wedge11.java"---------------------

      --------------------------start of file "getvars.java"---------------------
      import java.util.*;
      import java.io.*;

      class getvars {

          public static void main (String args[]) {

              //get all environment variables, and display some of them
              Properties pr = System.getProperties();

              Enumeration en = pr.propertyNames();

              while (en.hasMoreElements()) {

                  String keyword = (String)en.nextElement();
                  String variable;

                  if ((keyword.equals(new String("REQUEST_METHOD"))) ||
                      (keyword.equals(new String("CONTENT_LENGTH"))) ||
                      (keyword.equals(new String("QUERY_STRING")))) {

                      System.out.println("getvars: keyword:" + keyword);
                      variable = System.getProperty(keyword);
                      System.out.println("getvars: variable:" + variable);
                  }
              }
              System.out.println("getvars: done");
          }
      }
      --------------------------end of file "getvars.java"---------------------

            apalanissunw Anand Palaniswamy (Inactive)
            sgoodsunw Sheri Good (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: