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

/*CodeStart*/
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
/*CodeEnd*/

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(CreateXmlFile.java:23)

If any one faced similar issue and resolved please help.
10 comments
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
0
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.
0
Hi!

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?
0
Hello,

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: http://stackoverflow.com/questions/11677572/dealing-with-xerces-hell-in-java-maven, 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,
Alex.
0
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...

Code:

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

   // Code that was failing here
   
} finally {
  //Again changing the class loader to default
  Thread.currentThread().setContextClassLoader(currentCCL);
}
  

This way it worked for me...
But still i am looking for a better way to fix it. If you know please help
0
Hi!
Can I ask you to file a request for MPS in MPS youtrack & attach example project to it (if it is possible)
0
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.
0
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.
0

Please sign in to leave a comment.