RFR: 8335896: Source launcher should set TCCL

Christian Stein cstein at openjdk.org
Thu Jul 11 09:32:54 UTC 2024


On Tue, 9 Jul 2024 10:52:46 GMT, Christian Stein <cstein at openjdk.org> wrote:

> Please review this change to set the context class loader of the current thread to the in-memory class loader when the `java` launcher is invoked in source mode. Having the source launcher set the TCCL to the in-memory classloader is benefical for scenarious depending on the TCCL being set to the application-loading loader.
> 
> For example, the single-argument taking [`ServiceLoader.load(Class)`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/ServiceLoader.html#load(java.lang.Class)) method creates "a new service loader for the given service type, using the current thread's [context class loader](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#getContextClassLoader())."

I forgot to add two more files to the JBS issue. Namely the `module-info.java` file:

module test {
  uses Runnable;

  provides Runnable with
      test.Prog.Enclosed, test.TopLevel;
}

and also the `test/TopLevel.java` file:

package test;

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


With all those file in place, you get the following output using JDK 24:
`java --show-version test/Prog.java`

openjdk 24-ea 2025-03-18
[...]

With `Thread.currentThread().setContextClassLoader(Prog.class.getClassLoader());` activated in `Prog.java` you'll see the `Runnable`s being loaded and executed:
`java --show-version test/Prog.java`

openjdk 24-ea 2025-03-18
[...]
class test.Prog$Enclosed
class test.TopLevel

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

PR Comment: https://git.openjdk.org/jdk/pull/20097#issuecomment-2222462093


More information about the compiler-dev mailing list