How to fix ClassCastException when using jaxb dependency - java

I am developing jira server plugin which uses a 3rd party library which contains jaxb as a runtime dependency. Following is the pom xml file of that 3rd party library
<dependencies>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
And also a jaxb version (v2.3.0) is available in the jira server runtime. I am getting the following error
ClassCastException: attempting to cast jar:file:/C:/JiraServer/Jira%20Software/Atlassian/Jira/atlassian-jira/WEB-INF/lib/jaxb-api-2.3.0.jar!/javax/xml/bind/JAXBContext.class to
bundle://252.0:18/javax/xml/bind/JAXBContext.class. Please make sure that you are specifying the proper ClassLoader.
I tried to exclude the jaxb from the 3rd party library that I am using, but it did not worked for me. Can someone please advice how to resolve this issue.
Thanks!
Mifraz

Related

Java - Excluding Child Dependency in Maven

I have two project that names are payment-orchestrator and payment-connector.
payment-orchestrator is included payment-connector as maven dependency as shown below;
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${version.gson}</version>
</dependency>
<dependency>
<groupId>com.payment</groupId>
<artifactId>connector</artifactId>
<version>1.0.1</version>
</dependency>
payment-orchestrator has a gson library and also payment-connector contains an external jar that names integrator.jar but this jar use different versioned gson library. In this case, I am getting an error during calling JsonParser.parseString method like this
java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement;
I think, I have to exclude second gson library that exist in integrator.jar but couldn't because this library is in the jar. This code block doesn't work properly.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${version.gson}</version>
</dependency>
<dependency>
<groupId>com.payment</groupId>
<artifactId>connector</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
How can I handle this situation? Thank you for your answer.

Maven not downloading elasticsearch high level rest client dependency at all

I followed the usual procedure in adding the dependency for the es high level rest client, but it seems like maven is ignoring it completely. Here's my pom config -
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
We are also using spring boot too, but that should not be an issue here, as it is downloading all es dependencies except the high level rest client. The version is 6.8.7. Please help.
UPDATE:
I resolved this by creating a new project, basically there were some conflicts in the existing project and I am yet to debug that.
Please find snippet of my pom below and, important, I would highly recommend to start using version 7 of the Rest High Level Client library. We ran into major problems (mostly load-related) when using version 6 and Elastic decided for a full rewrite of their library.
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>

How to shade lucen-core so that I can use two versions of it to be used by ElasticSearch and Solr?

I have a project which needs to use ElasticSearch 7.6 and Solr 6.6 but these dependencies use different versions of lucene-core. ES comes with 8.4 but Solr comes with 6.6 and when Solr tries to use the newer one it will get random class not found errors for things as shown below:
java.lang.NoClassDefFoundError: org/apache/lucene/document/LegacyIntField
I have tried to shade but haven't had any luck and hope to get some help here :)
My dependencies in my POM:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>6.6.2</version>
</dependency>

javax.enterprise.inject.Vetoed Open web beans, which jar?

I'm wanting to try out Open Web Beans 1.6.2, but the jars it lists on it's website for adding CDI support to a Java SE application
openwebbeans-spi.jar
openwebbeans-impl.jar
geronimo-jcdi_1.0_spec.jar
geronimo-atinject_1.0_spec.jar
geronimo-interceptor_1.2_spec.jar
geronimo-annotation_1.2_spec.jar
Don't seem to contain the javax.enterprise.inject.Vetoed annotation, I've had to add cdi-api 1.2 as a dependency to resolve the issue, but I'm not sure if this is correct as all other CDI dependencies were resolved by the above? Here are the dependencies I have in my pom, is this correct?
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.2_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
This link seems to suggest support for #Vetoed was added in Open Web Beans 1.5.0
Please upgrade geronimo-jcdi_1.0_spec to jcdi_1.1 version
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
<version>1.0</version>
</dependency>
Although your problem is weird, because you have also following entry:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency
which also contains #Vetoed and thus should be enough. Does maven ignore it somehow?

CXF ClassNotFoundException: javax.ws.rs.MessageProcessingException

I'm trying to start a jax ws & rs server endpoints, I can get them started using rs-api 2.0 (and version 2.0.1) but when I try to make a request it throws
java.lang.NoClassDefFoundError: javax/ws/rs/MessageProcessingException
There are some other threads in SO regarding this matter but the suggestions don't work for me. Using any of the rs-api 2 milestone versions throws issues
Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/BadRequestException
I'm running this as a java application, not a webapp. Anyone have any ideas to try? Thanks
EDIT: My dependencies. I added jsr311 but that did not change the MessageProcessingException
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
CXF: version 2.7.0
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
</dependency>
rs-api version: 2.0
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
jsr311 version: 1.1.1
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
</dependency>
Faced the same issue, but later when I used 3.0.0-milestone2 of cxf-bundle with 2.1-m01 of javax.ws.rs-api, it worked like a charm.
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m01</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m10</version>
</dependency>
This version has support for javax.ws.rs.MessageProcessingException class.
I took this same error using Jersey client without JSON/JAXB providers to do the calls to server, when I configure Jettison provider the problem was solved. This erros also occour when I use a incompatible POJO to marshaling object to call service. check it. I hope this help.
The problem also occur when you have Classloader problems, check your maven dependecy hierarchy conflicts of jax-rs. In my case I need to use exlcusion filter in my pom.xml like
<exclusions>
<exclusion>
<artifactId>javax.ws.rs-api</artifactId>
<groupId>javax.ws.rs</groupId>
</exclusion>
</exclusions>
I made exclusion in cxf-bundle-jaxrs in another project that uses javax.ws.rs-api dependency. Using dependency of cxf-rt-frontend-jaxrs:2.7.11 was sufficient.
I had the same issue & I had to add this exclusion -
<exclusion>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
</exclusion>

Categories

Resources