Yeah, I've enable a logger for the Stub class and it shows your description when TCCL is used. However, I've changed the code locally to ClassLoader loader = Stub.class.getClassLoader(); and the logs always shows
ModuleClassLoader for Module "javax.rmi.api:main"
but the random error still persists.