Philip John Philip John - 6 months ago 64
Java Question

java.lang.NoClassDefFoundError: org/springframework/web/client/ResponseErrorHandler

I got this issue when I tried to open my deployed application in HEROKU. NoClassDefFoundError is being thrown for the Spring Framework class ResponseErrorHandler. I tried changing the xml and properties files, but in vain.


[33m2014-12-15T12:12:46.641119+00:00 heroku[web.1]:←[0m Starting process with command
java -Dserver.port=39847 -Ddatabase.url=postgres://genreimfcnjetj:Swt7eGbTANi2-X7tBru0wTX4GT@ec2-54-235-76-206.compute-1.amazonaws.com:5432/d9108hum0pg29o -Xss512k -XX:+UseCompressedOops -Dspring.profiles.active=production -jar target/*.jar

[33m2014-12-15T12:12:47.517755+00:00 app[web.1]:←[0m at java.lang.Class.getDeclaredMethods0(Native Method)
[33m2014-12-15T12:12:47.518480+00:00 app[web.1]:←[0m at java.lang.Class.privateGetMethodRecursive(Class.java:3035)
[33m2014-12-15T12:12:47.517563+00:00 app[web.1]:←[0m Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/web/client/ResponseErrorHandler
[33m2014-12-15T12:12:47.518545+00:00 app[web.1]:←[0m at java.lang.Class.getMethod0(Class.java:3005)
[33m2014-12-15T12:12:47.519084+00:00 app[web.1]:←[0m at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
[33m2014-12-15T12:12:47.319339+00:00 app[web.1]:←[0m Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Djava.rmi.server.useCodebaseOnly=true
[33m2014-12-15T12:12:47.519022+00:00 app[web.1]:←[0m at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
[33m2014-12-15T12:12:47.518428+00:00 app[web.1]:←[0m at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
[33m2014-12-15T12:12:47.518938+00:00 app[web.1]:←[0m at java.lang.Class.getMethod(Class.java:1771)
[33m2014-12-15T12:12:47.519224+00:00 app[web.1]:←[0m Caused by: java.lang.ClassNotFoundException: org.springframework.web.client.ResponseErrorHandler
[33m2014-12-15T12:12:47.520145+00:00 app[web.1]:←[0m at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
[33m2014-12-15T12:12:47.519277+00:00 app[web.1]:←[0m at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
[33m2014-12-15T12:12:47.519388+00:00 app[web.1]:←[0m at java.security.AccessController.doPrivileged(Native Method)
[33m2014-12-15T12:12:47.520395+00:00 app[web.1]:←[0m at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[33m2014-12-15T12:12:47.520237+00:00 app[web.1]:←[0m at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[33m2014-12-15T12:12:47.519339+00:00 app[web.1]:←[0m at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
[33m2014-12-15T12:12:47.520312+00:00 app[web.1]:←[0m at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[33m2014-12-15T12:12:47.520441+00:00 app[web.1]:←[0m ... 7 more
[33m2014-12-15T12:12:48.201159+00:00 heroku[web.1]:←[0m Process exited with status 1
[33m2014-12-15T12:12:48.209235+00:00 heroku[web.1]:←[0m State changed from starting to crashed


Can I have some HELP please.

Sharing my classes relating to ResponseErrorHandler.

CustomerErrorResponseHandler.java

package rentitnew;

import java.io.IOException;

import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;

class CustomResponseErrorHandler implements ResponseErrorHandler {
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return errorHandler.hasError(response);
}

@Override
public void handleError(ClientHttpResponse response) throws IOException {
}
}


Application.java

package rentitnew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

@Autowired
private WebMvcProperties mvcProperties = new WebMvcProperties();
@Autowired
Credentials credentials;
@Autowired
ClientHttpRequestFactory basicSecure;

@Bean
public RestTemplate restTemplate() {
RestTemplate _restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
_restTemplate.setMessageConverters(messageConverters);
_restTemplate.setErrorHandler(new CustomResponseErrorHandler());
_restTemplate.setRequestFactory(basicSecure);
return _restTemplate;
}

@Bean
public ClientHttpRequestFactory requestFactory() {
return new BasicSecureSimpleClientHttpRequestFactory();
}

@Bean
@ConfigurationProperties(locations = "classpath:META-INF/integration/credentials.yml")
public Credentials getCredentials() {
return new Credentials();
}

public static class Credentials {
private Map<String, Map<String, String>> credentials = new HashMap<>();

public Map<String, Map<String, String>> getCredentials() {
return this.credentials;
}
}

public static void main(String[] args) {

SpringApplication.run(Application.class, args);
}
}


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>rentitnew</groupId>
<artifactId>rentitnew</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>rentitnew</name>
<description>Rentit Project</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.8</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.16.0.RELEASE</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>rentitnew.Application</start-class>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<systemProperty>
<name>spring.profiles.active</name>
<value>production</value>
</systemProperty>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<reportsDirectory>shippable/testresults</reportsDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>




Procfile

web: java -Dserver.port=$PORT -Ddatabase.url=$DATABASE_URL $JAVA_OPTS -Dspring.profiles.active=production -jar target/*.jar


Can I have some suggestions how I can change the code/other files so that I can open my application.
Thanks in advance!

Answer

Well, I see I haven't answered this question yet. So even though it is late, I will provide the fix I had made to solve this issue.

Apart from the change proposed by Jens, I added the following plugin into the plugins tag in pom.xml.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

I had accidently removed this plugin from pom.xml which caused the issue above.

Comments