Help: ContextClassLoader in jtreg tests

David Holmes David.Holmes at oracle.com
Mon Aug 8 02:46:26 UTC 2011


Hi Max,

Weijun Wang said the following on 08/06/11 00:19:
> I have two files: A.java and B.java
> 
> /*
>  * @test
>  * @build A B
>  * @run main A
>  */
> public class A {
>     public static void main(String[] args) throws Exception {
>         Class.forName("B", true, 
> Thread.currentThread().getContextClassLoader());
>     }
> }
> 
> public class B {
> }
> 
> Now jtreg A.java shows:
> 
>    java.lang.ClassNotFoundException: B
> 
> How can I get B visible in A's ContextClassLoader?

It isn't A's context class loader it is the current threads. If the 
above isn't working then either:

a) A is not being loaded by the application (aka system) classloader 
(jtreg is using a custom loader); or
b) jtreg has explicitly changed the current thread's context classloader

You would need to investigate what A.class.getClassLoader() reports, and 
what Thread.currentThread().getContextClassLoader() reports.

> Don't tell me to change to Class.forName("B"), that line is copied from 
> JDK that is called by a real test.
> 
> Or, the JDK code itself is problematic and it should fallback to another 
> ClassLoader?

It's unclear why the code/test needs to use the context classloader here 
(or why/how it knows the context loader is the correct loader).

Cheers,
David

> Thanks
> Max



More information about the core-libs-dev mailing list