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

Source launcher should set TCCL

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 24
    • None
    • tools
    • None
    • b08

      The source launcher creates an in-memory classloader to access compiled classes at runtime. It doesn't set it as the Thread Context Class Loader though. That's why for example the following program doesn't print anything, until either the comment-out line is included:

      ```
      module test {
        uses Runnable;

        provides Runnable with test.Prog.Enclosed;
      }
      ```

      ```
      package test;

      public class Prog {
        public static class Enclosed implements Runnable {
          @Override
          public void run() {
            System.out.println(getClass());
          }
        }

        public static void main(String... args) {
          // Thread.currentThread().setContextClassLoader(Prog.class.getClassLoader());
          java.util.ServiceLoader.load(Runnable.class).forEach(Runnable::run);
        }
      }
      ```
      or the `ServiceLoader.load(Class, ClassLoader)` variant is used with `Prog.class.getClassLoader()` as the second argument.

      Having the source launcher set the TCCL to the in-memory classloader would keep the program clean(er) and would also be benefical for other scenarious depending on the TCCL being set to the application-loading loader.

            cstein Christian Stein
            cstein Christian Stein
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: