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

(cl) ClassLoader.getSystemResource returns URL with spurious leading slash

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs



      Name: nt126004 Date: 06/12/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Windows NT Version 4.0
      Service Pack 6a

      A DESCRIPTION OF THE PROBLEM :
      ClassLoader.getSystemResource returns a URL object whose
      getFile method returns a string with an unwanted leading
      slash. In particular, if the resource is a Windows
      executable, the returned string will not work when passed
      to Runtime.exec.

      I guess this could alternatively be viewed as a bug in
      Runtime.exec for not being able to handle a "valid" file
      name.

      It seems that the URL might be a valid URL according to the rfc,
      but if so Runtime should be able to handle it. If the URL that is
      generated is not correct, then ClassLoader needs to be fixed.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create and compile Java test program

      2. Create and compile C test program. Leave executable in
      same directory as Test.class.

      #include <stdio.h>
      main() {
        printf("Hello\n");
      }

      3. Run Java test program:

      java Test

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected that file name gotten from URL.getFile should work
      when passed to Runtime.exec. Instead got file name with
      spurious leading slash that is not acceptable to
      Runtime.exec. Removal of offending slash produces file
      name that is acceptable.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      > java Test
      getProtocol() = file

      getFile() = /D:/njoneill/java/UrlBug/Hello.exe

      getPath() = /D:/njoneill/java/UrlBug/Hello.exe

      exec without slash

      exec with slash

      java.io.IOException: CreateProcess: /D:/njoneill/java/UrlBug/Hello.exe error=123

      at java.lang.Win32Process.create(Native Method)

      at java.lang.Win32Process.<init>(Win32Process.java:61)

      at java.lang.Runtime.execInternal(Native Method)

      at java.lang.Runtime.exec(Runtime.java:546)

      at java.lang.Runtime.exec(Runtime.java:413)

      at java.lang.Runtime.exec(Runtime.java:356)

      at java.lang.Runtime.exec(Runtime.java:320)

      at Test.main(Test.java:19)

      Exception in thread "main"


      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.*;
      public class Test {
          public static void main(String[] args)
      throws Exception {

      // Hello.exe is just a simple C prog in same dir as Test.class
      URL url = ClassLoader.getSystemResource("Hello.exe");

      System.out.println("getProtocol() = " + url.getProtocol());
      System.out.println("getFile() = " + url.getFile());
      System.out.println("getPath() = " + url.getPath());

      // Clip the leading slash and all is well
      System.out.println("exec without slash");
      Runtime.getRuntime().exec(url.getFile().substring(1));

      // Leave the slash and generate an exception
      System.out.println("exec with slash");
      Runtime.getRuntime().exec(url.getFile());

          }
      }


      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Determine if you are running under Windows (by looking at
      os.name system property perhaps) and remove leading slash
      before handing to Runtime.exec.
      (Review ID: 148166)
      ======================================================================

            iris Iris Clark
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: