BZ-1038229 - JMS API is unable to load a class in the deployment with some requests

- Some of this code is also for BZ-1026402, since it seems like something
  changed with the classloader to break the fixes that were previously
  sufficient for it.
(cherry picked from commit b229a4a3218097920a95dfac31bcf48415b06cac)