RFR: 8335896: Source launcher should set TCCL

Christian Stein cstein at openjdk.org
Tue Jul 16 08:15:56 UTC 2024


On Tue, 16 Jul 2024 08:08:48 GMT, Christian Stein <cstein at openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java line 204:
>> 
>>> 202:             ClassLoader loader = context.newClassLoaderFor(parentLoader, firstClassName);
>>> 203:             firstClass = Class.forName(firstClassName, false, loader);
>>> 204:             Thread.currentThread().setContextClassLoader(loader);
>> 
>> Are you sure this it the right place? It might be, but seeing it in a try-catch is surprising. It should be set before the initializer for the main class runs, which might be here but checking.
>
> Would the constructor of `MemoryClassLoader` be a better place?
> 
> Because `context.newClassLoaderFor()` has two exit-points where an instance of `MemoryClassLoader` is created.

The try-catch cares for `ClassNotFoundException` instances thrown by `Class.forName()` - which doesn't initialize the main class. But swapping the lines to read:

            Thread.currentThread().setContextClassLoader(loader);
            firstClass = Class.forName(firstClassName, false, loader);

should better ensure that the TCCL is set before the initializer for the main class runs.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/20097#discussion_r1678956482


More information about the compiler-dev mailing list