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

java.lang.Runtime.exec doesn't change working directory

XMLWordPrintable

    • linux-beta
    • x86
    • linux
    • Verified



      Name: skR10017 Date: 04/26/2000



      Java 2 Platform SE v1.3 Specification reads:

      public Process exec(String command,String[] envp,File dir) throws IOException

      ------ cut -----

      Parameters:
                  command - a specified system command.
                  envp - array of strings, each element of which has environment variable settings in format
                  name=value.
                  dir - the working directory of the subprocess, or null if the subprocess should inherit the working
                  directory of the current process.

      -----------------------------------------------------

      The following program tests correct work of java.lang.Runtime.exec(String cmd, String [] envp, File path)
      method. The test executes /bin/pwd with root working directory.
      The expected output of /bin/pwd is "/" but under jdk1.3.0beta for linux it produces
      instead of "/" the value of java machine working directory.
      This means that java machine didn't change working directory during
      Runtime.exec method invocation.

      The test passes on jdk1.3.0Z for solaris.

      --------------------------------- test.java ---------
      import java.io.*;

      public class test {
          public static void main(String [] args)
          {
              try
              {
                  Process p;
                  String cmd="/bin/pwd";
                  File f=new File("/");

                  p = Runtime.getRuntime().exec(cmd, null, f);

                  BufferedReader in = new BufferedReader
                      (new InputStreamReader(p.getInputStream()));

                  String s = in.readLine();
                  if(s.compareTo("/")!=0)
                        System.out.println("Error: directory differes from expected:"+s);
                  else System.out.println("Passed.");
              }
              catch(Exception e)
              {
                  System.out.println(e);
              }
          }
      }
      ----------------------------- output ----------------------------------
       on Linux:

      [kotl@linux-6 exec]$ pwd
      /home/kotl/src/reg_res/exec
      [kotl@linux-6 exec]$ java test
      Error: directory differes from expected:/home/kotl/src/reg_res/exec
      [kotl@linux-6 exec]$

       on Solaris:

      bash-2.00$ pwd
      /home/kotl/src/reg_res/exec
      bash-2.00$ java test
      Passed.
      bash-2.00$

      ======================================================================

      ======================================================================

            caustinsunw Calvin Austin (Inactive)
            kotlsunw Kotl Kotl (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: