Java Class Cast Exception in MPS <<Class Loader Issue>>

Hello All,
I have created a post processing script in the generator which call a java static method.
I am having the below code in that method where it is failing

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Error Message:
java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
 at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
 at com.util.CreateXmlFile.testMethod(

If any one faced similar issue and resolved please help.
i guess it is a java problem? you just import two model which just have the same class: DocumentBuilderFactory,
you need delete the import in dependency
Hi John, Thanks for the reply but the problem was not with the imports rather with the class loader, so by overriding the default class loaded i resolve the issue.

I think #Jetbrains team should look into the class loaded issue as every time overriding the class loader is not a good practice.

Can you please explain what did you change in the class loader (and how did you override it??)

Both mentioned classes are available by-default through the classloader of JDK bootstrap solution. So, if you have another solution loading these classes and by a chance include this solution into dependency graph of the generator this can lead to such problems.

Can you check that you do not have any solutions loading DocumentBuilderFactory/DocumentBuilder classes inside your project?

I did see such exception some time ago – in my case the reason was some classes conflicts between the particular jdk and xerces.jar. There is a certain mixup with xerces jars:, which MPS sometimes does not handle well. (rarely, but happens)

Anyway I suppose you have several versions of the class in your class path (in the different jars I assume).
Could you please give some details on your particular solution? If you examined deeply this problem, could you also tell your advances on your investigation?

Thank you,
Hi Alex S,
When i run the java class separately it runs but when i call it from the mapping script it was not working so i just replace the default class loader to the class loader of the java class and it worked...


//Getting the current classLoader
ClassLoader currentCCL = Thread.currentThread().getContextClassLoader();  
try {
  //Changing it to the current class loader

   // Code that was failing here
} finally {
  //Again changing the class loader to default

This way it worked for me...
But still i am looking for a better way to fix it. If you know please help
Can I ask you to file a request for MPS in MPS youtrack & attach example project to it (if it is possible)
Hello Alex,

Certainly i don't know what is MPS youtrack, so if you can give me further details on how to add it i will do.
Hi Vaclav/Alex,

I have created Isseue "MPS-21776" in the youtrack, i have not attach sample project there, but writes explanation with code where it fails under the issues description field, hope that will be sufficient! if not then please let me know.

Please sign in to leave a comment.