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.
```
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.
- links to
-
Commit(master) openjdk/jdk/ad498f57
-
Review(master) openjdk/jdk/20097