Same code gives an error (org.apache.xerces.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory) when used in Jira plugin

When I create a project outside of Jira and run the code, it works without a problem, but when including it in a Jira plugin, then I get that error:
org.apache.xerces.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory

I saw in other forums that as a solution usually all the dependencies of xerces:xercesImpl and xml-apis:xml-apis are excluded. I have tried this and it did not help.

Does anyone know anything about this kind of problem? Thank you