java.lang.LinkageError: ClassCastException:attempting to castjar: javax.ws.rs-api-2.0.1.jar

huangapple go评论59阅读模式
英文:

java.lang.LinkageError: ClassCastException:attempting to castjar: javax.ws.rs-api-2.0.1.jar

问题

The error message you provided indicates a java.lang.LinkageError with a ClassCastException while attempting to cast a class. This issue seems to be related to a conflict with the Jersey (JAX-RS) library versions in your project.

To resolve this issue, you should ensure that you have consistent and compatible versions of Jersey libraries throughout your project. In your pom.xml file, you have a mix of Jersey 1.x and Jersey 2.x dependencies, which could be causing the problem.

Here are some steps to consider:

  1. Remove Jersey 1.x dependencies from your pom.xml if you are not using them. Specifically, the dependencies related to com.sun.jersey and javax.ws.rs.

  2. Ensure that you have only one version of Jersey (preferably Jersey 2.x) in your dependencies, and make sure it's compatible with Java 8 and Tomcat 8.

  3. Check for any other conflicting dependencies in your project that might be causing this issue.

  4. Rebuild and redeploy your project to Tomcat after making these changes.

By ensuring consistency in your Jersey dependencies, you should be able to resolve this ClassCastException issue.

英文:

I am running below rest API in my code and it gives me the below error. I am not sure whether this is an issue with the jar. Please help me .

java.lang.LinkageError: ClassCastException: attempting to castjar:file:/C:/apache-tomcat-8.5.9/wtpwebapps/searchextractweb/WEB-INF/lib/javax.ws.rs-api-2.0.1.jar!/javax/ws/rs/ext/RuntimeDelegate.class to jar:file:/C:/apache-tomcat-8.5.9/wtpwebapps/searchextractweb/WEB-INF/lib/javax.ws.rs-api-2.0.1.jar!/javax/ws/rs/ext/RuntimeDelegate.class
	javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:146)
	javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
	javax.ws.rs.core.MediaType.valueOf(MediaType.java:179)
	com.sun.jersey.api.client.PartialRequestBuilder.type(PartialRequestBuilder.java:92)
	com.sun.jersey.api.client.WebResource.type(WebResource.java:343)
	com.tlr.searchextract.workflow.Workflow.retrieveSearchInfo(Workflow.java:1208)
	com.tlr.searchextract.workflow.Workflow.createWorkflowRequest(Workflow.java:275)
	com.tlr.searchextract.messages.SearchExtractEventHandler.createNewWorkflowRequest(SearchExtractEventHandler.java:675)
	com.tlr.searchextract.messages.SearchExtractEventHandler.processRequest(SearchExtractEventHandler.java:134)
	com.tlr.searchextract.messages.SearchExtractEventHandler.processMessage(SearchExtractEventHandler.java:65)
	com.tlr.searchextract.messages.MessageHandler.routeMessage(MessageHandler.java:92)
	com.tlr.searchextract.messages.MessageHandler.processMessages(MessageHandler.java:64)
	com.tlr.searchextract.servlet.RequestModel.insertCurrentRequest(RequestModel.java:190)
	com.tlr.searchextract.servlet.SEControllerServlet.insertRequestTemplate(SEControllerServlet.java:1344)
	com.tlr.searchextract.servlet.SEControllerServlet.performTask(SEControllerServlet.java:1941)
	com.tlr.searchextract.servlet.SEControllerServlet.doPost(SEControllerServlet.java:90)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I have the below code for executing REST API

private void retrieveSearchInfo() {
// find out what type of workflow to create
searchType =
document
.getElementsByTagName("search.type")
.item(0)
.getFirstChild()
.getNodeValue();
try {
excludeMetaDoc =
document
.getElementsByTagName("exclude.metadoc")
.item(0)
.getFirstChild()
.getNodeValue();
} catch (Exception e) {
excludeMetaDoc = "";
}
try {
searchGroup =
document
.getElementsByTagName("search.group")
.item(0)
.getFirstChild()
.getNodeValue();
} catch (Exception e) {
searchGroup = "";
}

try{
imageDoc =
	document
		.getElementsByTagName("search.imagedoc")
		.item(0)
		.getFirstChild()
		.getNodeValue();
}catch (Exception e) {
	imageDoc = "";
}		

//add the term "search" to the value of the searchLevel
//vaiable in order to fit the needs of the LTC request
searchLevel =
	document
		.getElementsByTagName("search.level")
		.item(0)
		.getFirstChild()
		.getNodeValue();

if (searchLevel.equalsIgnoreCase("collection set")) {
	searchLevel = "collectionset";
}

//collection or collection set name
searchName =
	document
		.getElementsByTagName("search.name")
		.item(0)
		.getFirstChild()
		.getNodeValue();

searchNovusVersion =
	document
		.getElementsByTagName("search.novus.version")
		.item(0)
		.getFirstChild()
		.getNodeValue();

searchNovusEnvironment =
	document
		.getElementsByTagName("search.novus.environment")
		.item(0)
		.getFirstChild()
		.getNodeValue();

//check to see if the user wants either all of the guids
//for a collection or a collection set	
if (searchType.equalsIgnoreCase("all guids")
	|| searchType.equalsIgnoreCase("document count")) {
	if("Norm".equalsIgnoreCase(searchGroup))
		queryText = "=n-relbase";
	else
		queryText = "=n-document";

	queryType = "boolean";

} else {

	queryText =
		document
			.getElementsByTagName("search.query.text")
			.item(0)
			.getFirstChild()
			.getNodeValue();

	//escapte special characters
	//		escape any reserved characters
	// Problem using an ampersand (&) in the Search query.  Maestro translates it to an entity in the relevant data.  Need to use the word "and". 
	queryText = escapeXML(queryText, "&", "and");
	//			queryText = escapeXML(queryText, "&", "&");
	queryText = escapeXML(queryText, "<", "<");
	queryText = escapeXML(queryText, ">", ">");
	queryText = escapeXML(queryText, "'", "'");
	queryText = escapeXML(queryText, "\"", """);

	//find the search type boolean or natural
	queryType =
		document
			.getElementsByTagName("search.query.type")
			.item(0)
			.getFirstChild()
			.getNodeValue();
}

try {
	searchOutputResource =
		document
			.getElementsByTagName("search.output.resource")
			.item(0)
			.getFirstChild()
			.getNodeValue();

	searchOutputPath =
		document
			.getElementsByTagName("search.output.path")
			.item(0)
			.getFirstChild()
			.getNodeValue();

	searchOutputPrefix =
		document
			.getElementsByTagName("search.output.file.prefix")
			.item(0)
			.getFirstChild()
			.getNodeValue();

} catch (Exception e) {
	searchOutputResource = "";
	searchOutputPath = "";
	searchOutputPrefix = "";

	if (searchOutputPrefix != null) {
		if (searchOutputPrefix.length() == 0) {
			searchOutputPrefix = "se";
		}
	} else {
		searchOutputPrefix = "se";
	}
	//e.printStackTrace();
}

//now get the resource signon and password

String output="";

try {

	Client client = Client.create();
	System.out.println("resourceName: "+searchOutputResource);
    WebResource webResource = client.resource("http://localhost:8080/searchextract/webapi/resource?isGroupAndResource=true&groupId="
		        + requestGroup + "&resourceName=" + searchOutputResource);

    ClientResponse response = webResource.type("application/json").get(ClientResponse.class);
if (response.getStatus() != 200)
{
	throw new RuntimeException("Failed : HTTP error code : "
	     + response.getStatus());
}

output = response.getEntity(String.class);

	ResultSetIterator rsi = new ResultSetIterator(output);

	searchOutputResourceUser = rsi.getFieldValue("resource_signon");
	searchOutputResourcePass = rsi.getFieldValue("resource_password");

} catch (RemoteException re) {
	System.err.println(re.detail.getMessage());
	if (re.detail.getMessage().indexOf("DuplicateKeyException") > -1) {
		//throw new Exception("Duplicate record");
	}
} catch (NamingException e) {
	System.err.println("NamingException: " + e.getMessage());
	System.err.println("Root cause: " + e.getRootCause());
	System.err.println("Explanation: " + e.getExplanation());
} 
catch (Exception e) {
	System.err.println(e.getMessage());
}

}

Below is the pom.xml file

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tlr.searchextractproject</groupId>
<artifactId>parent-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.tlr.searchextractproject</groupId>
<artifactId>searchextract</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>searchextract</name>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-bundle -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-guava</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.9.1</version>
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>8.5.9</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.9</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>8.5.9</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibm</groupId>  
<artifactId>com.ibm.mq</artifactId>
<version>6.0.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm/com.ibm.mqjms -->
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.mqjms</artifactId>
<version>6.0.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.resource/connector -->
<dependency>
<groupId>javax.resource </groupId>
<artifactId>connector</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.dhbcore </artifactId>
<version>7.1.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>searchextract</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

This exception is thrown while executing the below line
ClientResponse response = webResource.type("application/json").get(ClientResponse.class)
I am using Java 8 and Tomcat 8

答案1

得分: 3

I have faced the same error with my code and could resolve by using jersey dependency version 2.34

POM dependency

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>3.0.0</version>
</dependency>

My Error Message

2021-09-22 08:32:23.839 INFO 10 --- [http-nio-9070-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.LinkageError: ClassCastException: attempting to castjar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class to jar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class] with root cause

java.lang.LinkageError: ClassCastException: attempting to castjar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class to jar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class
    at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:105)

I updated the dependency version of jersey-client and jersey-common to 2.34, and the issue was resolved.

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.34</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.34</version>
</dependency>
英文:

I have faced the same error with my code and could resolve by using jersey dependency version 2.34

POM dependency

&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
&lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
&lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
&lt;artifactId&gt;jersey-common&lt;/artifactId&gt;
&lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;

My Error Message

2021-09-22 08:32:23.839 INFO 10 --- [http-nio-9070-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.LinkageError: ClassCastException: attempting to castjar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class to jar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class] with root cause
java.lang.LinkageError: ClassCastException: attempting to castjar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class to jar:file:/faw-qa-api/target/faw-qa-api-1.0-SNAPSHOT.jar!/javax/ws/rs/client/ClientBuilder.class
at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:105)

I updated the dependency version of jersey-client and jersey-common to 2.34 and the issue was resolved.

&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
&lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
&lt;version&gt;2.34&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
&lt;artifactId&gt;jersey-common&lt;/artifactId&gt;
&lt;version&gt;2.34&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年7月29日 10:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63145348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定