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

Class.getResource works incorrectly under plugin and appletviewer

XMLWordPrintable

    • rc1
    • generic
    • generic
    • Verified

      Name: skR10005 Date: 11/09/2001

      It seems Class.getResource works incorrectly due to some optimization of loading resources in appletviewer ClassLoader.

      Class.getResource cannot find a valid resource after
      Class.getResourceAsStream(null) invocation.

      JCK tests:
      api/java_lang/Class/index.html#Reflect[Class0915]
      api/java_lang/Class/index.html#Reflect[Class0919]

      To reproduce this failure the simple test can be used:
      ========================A.java============================
      import java.applet.*;

      public class A extends Applet {
         public static void main( String[] args ) {
            new A().init();
         }
         public void init() {
      // commenting out this part of code makes the test pass
              try {
                  this.getClass().getResourceAsStream(null);
                  System.out.println(" Failed: NullPointerException expected");
              } catch (NullPointerException npe) {
                  System.out.println( "1) Passed: OKAY" );
              }
      // comments should be ended here
              try {
                  java.net.URL url = this.getClass().getResource("resource");
                  if (url == null) {
                      System.out.println( "2) Failed: url == null" );
                  } else {
                      if ( ! url.getFile().endsWith( "resource") ) {
                          System.out.println("getResource() returned: "
                                  + this.getClass().getResource("resource").toString());
                      }
                      System.out.println( "2) Passed: OKAY" );
                  }
              } catch (NullPointerException npe) {
                  System.out.println( "Failed: NullPointerException " );
                  npe.printStackTrace();
              }
         }
      }
      ======================A.html=============================
      <html>
      <body>
      <applet code="A"
      width=500
      height=500>
      </applet>
      </body>
      </html>
      ======================resource===========================
      This is a simple resource
      =========================================================
      The files "resource", "A.html", "A.java" should be placed into
      the same directory.

      $javac -d . A.java
      $appletviewer -J-version
      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b85)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b85, mixed mode)

      $appletviewer A.html
      1) Passed: OKAY
      2) Failed: url == null

      Note that removing/commenting out the code between // comments
      in example results in the test is passed:
      $javac -d . A.java
      $appletviewer A.html
      2) Passed: OKAY

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

            mcolburnsunw Michael Colburn (Inactive)
            serjsunw Serj Serj (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: