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

Java Runtime.exec() tries to execute a directory

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.7, 1.2.0
    • core-libs
    • None
    • generic
    • solaris_2.6

      Copy of bug 4199993 in java_solaris category - fixed in Solaris Java,
      needs to be fixed in reference platforms.
      --------------------------------------------------------------------------
      If Runtime.exec() locates a directory (with execute permission, I
      assume) in the PATH, it will try to execute it and will get an error.
      For example, I have a Java program which calls
      Runtime.exec("netscape"). If I run this program, it normally starts
      netscape. However, if I run this program in a location which contains
      a directory called "netscape", I get an error.

      From the command line, if I type "netscape" from the same location, it
      starts netscape despite the presence of the "netscape" directory.

      Here's how to reproduce the problem.

      1) Make sure "." is the first thing in your PATH.

      2) Create an executable script called "foo" and place it anywhere on
      your path except the directory in which we will be performing the
      test. The script can be something like:

      #!/bin/ksh
      print "Hello from foo!"

      3) Create a new directory for testing. Compile this Java program in
      that directory:

      import java.io.*;

      public class Main
      {

      static public void
      main(
      String[] args)
      throws IOException, InterruptedException
      {
      String[] command = {
      "foo"
      };

      Runtime runtime = Runtime.getRuntime();

      Process process = runtime.exec(command);
      process.waitFor();

      System.out.println("Exit value = " + process.exitValue());
      }

      }


      4) Create a sub-directory called "foo". Make sure it has execute
      permission for the owner (this should be the default setting).

      5) From the command line, try this sequence, execute "foo" and verify
      that the script in step (2) is executed properly.

      6) Run the Java program. The output will be:

      java.io.IOException: foo: cannot execute
      at java.lang.Runtime.exec(Compiled Code)
      at java.lang.Runtime.exec(Compiled Code)
      at Main.main(Compiled Code)
      *** Error code 1

      7) Delete the "foo" directory and re-run the program. The output is
      now

      Exit value = 0

      8) Re-create the "foo" directory, but now place it somewhere else on
      the PATH, *before* the "foo" script. Re-run the program. The output
      is again:

      java.io.IOException: foo: cannot execute
      at java.lang.Runtime.exec(Compiled Code)
      at java.lang.Runtime.exec(Compiled Code)
      at Main.main(Compiled Code)
      *** Error code 1


      The presence of a directory should not affect whether a Java program
      can execute a sub-process or not. This is specially bad since the
      directory can be anywhere in the PATH ahead of the desired executable.

            apalanissunw Anand Palaniswamy (Inactive)
            foliversunw Fred Oliver (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: