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:
If any one faced similar issue and resolved please help.
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.
Please sign in to leave a comment.
you need delete the import in dependency
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: 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.
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
Can I ask you to file a request for MPS in MPS youtrack & attach example project to it (if it is possible)
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.
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.