Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError error creating RestHighLevelClient bean

I'm getting an error creating a HighEndRestClient bean in a SpringBoot app. I have done a test 'app' where I checked I can instantiate the objects I want and then make the calls I want to and I am now making baby steps into making a new application.

I have these dependencies in the pom

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>5.6.3</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>5.6.3</version>
    </dependency>

and I have written this very basic code in a configuration class which doesn't do much yet

@Configuration
@PropertySource(value = "classpath:application.properties")
@EnableElasticsearchRepositories(basePackages = "com.indexbuilder.es.repo")
public class ElasticsearchConfiguration {
    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;


    @Bean
    public RestClientBuilder coreBuilder() throws Exception {
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setMaxRetryTimeoutMillis(10000);

        builder.setFailureListener(new RestClient.FailureListener() {
            @Override
            public void onFailure(HttpHost host) {
                System.out.println("FAILURE !!!! FailureListener HAS WOKEN UP!!!! CREATYE A FAILURE LISTENER BEAN" );
            }
        });

        return builder;
      }

    @Bean
    public RestClient restLowLevelClient() throws Exception{
        RestClient restClient = coreBuilder().build();
        return restClient;
    }

This works fine as far as I can see (I haven't done much with it yet...)

when I add this (initially I was passing in the RestClient bean but now I'm temporarily creating a local object for more clarity)

    @Bean
    public RestHighLevelClient restHighLevelClient() throws Exception{
        RestClient restClient = coreBuilder().build();
        RestHighLevelClient client = new RestHighLevelClient(restClient);
        return client;
    }

I get this java.lang.NoClassDefFoundError error

=========|_|==============|___/=///_/ :: Spring Boot :: (v1.5.1.RELEASE)

[WARNING] java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restHighLevelClient' defined in class path resource [com/indexbuilder/configuration/ElasticsearchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:598) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1140) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:525) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:744) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) at com.indexbuilder.SpringBootStartUpConfig.main(SpringBootStartUpConfig.java:84) ... 6 more Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ... 24 more Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/action/main/MainRequest at com.indexbuilder.configuration.ElasticsearchConfiguration.restHighLevelClient(ElasticsearchConfiguration.java:95) at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a.CGLIB$restHighLevelClient$1() at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a$$FastClassBySpringCGLIB$$2b29ad7b.invoke() at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) at com.indexbuilder.configuration.ElasticsearchConfiguration$$EnhancerBySpringCGLIB$$62f74d9a.restHighLevelClient() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 25 more Caused by: java.lang.ClassNotFoundException: org.elasticsearch.action.main.MainRequest at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 36 more

Can anyone point me in the right direction ?

like image 935
gringogordo Avatar asked Oct 20 '17 18:10

gringogordo


1 Answers

You probably need the core dependency as well:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.6.3</version>
</dependency>

A NoClassDefFoundError is generally a configuration error - it means that the code you use references a certain class, but the class itself isn't in the classpath. In this case, this might also be a dependency management error in the relevant Elasticsearch poms themselves, as they should include the needed classes - but there's not much you can do about that other than perhaps file an issue.

like image 59
Piotr Wilkin Avatar answered Oct 12 '22 07:10

Piotr Wilkin