Version 5 and Java not working

Ok, i am looking at using ElasticSearch, via Java, for one of my applications,
and have installed version 5.0.0 and have it running ok, in about 10 mins
i have now copied, the Jar file down, into my test app, (netbeans is my development environment 8.0.2) , using start Jar file elasticsearch-5.0.0.jar no marven
and using the example from the docs/web page

public static void main(String[] args)
{

    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300));
    client.close();
}

and i am getting a comple error on PreBuiltTransportClient, and it does not apper to be in the Jar files, or am i missing some thing.
or is there another example else where, that does not use "PreBuiltTransportClient"

This class belongs to the Transport jar. See https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_maven_repository.html

Ok so I'm having the same problem. I've added the transport library through the pom entry shown, but the reference to Settings.EMPTY is complaining that it can't be resolved. Next idea?

May be refresh your IDE or run mvn clean install?
What is the exact error?

I'm using Eclipse, and have refreshed the project, forced clean several times. The portion of text of the example with the problem is:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)

The word EMPTY is highlighted and when I hover over it this is the exact message:

"EMPTY cannot be resolved or is not a field"

This is generally the message when the object definition doesn't exist in the project.
However, if I purposefully change the reference to something else, such as "EMPTYS", in addition to getting the same message (e.g., "... cannot be resolved or is not a field") it does prompt me to change it to "EMPTY_PARAMS". When I do that, however, then the error changes to highlight the entire constructor statement:

new PreBuiltTransportClient(Settings.EMPTY_PARAMS)

and says "The constructor PreBuiltTransportClient(ToXContent.Params) is undefined"

My imports in this class include:
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

Please format your code using </> icon as explained in this guide. It will make your post more readable.

I just checked on my end and as soon as I add the following in my pom.xml file:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.0.2</version>
</dependency>

I can write this code:

new PreBuiltTransportClient(Settings.EMPTY)
      .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));

If it does not work on your end, then check your IDE, your maven settings, look at eclipse messages.

What does mvn dependency:tree is saying?

I changed the version to 5.0.2 (although just last night the example gave me 5.0.1 as the version to use - fyi). Same problem. Then as I was trying to create a settings object directly, I discovered that there was some weird conflict between your code and the version of SpringBoot I had been trying to use (1.3.5). I upgraded to 1.4.2 and made a few other adjustments (had an unrelated LOGGER issue with the change) and now your code compiles and I can run the demonstration. Thanks for the hint...

I spoke too soon. While I compiled correctly, I'm getting a run time error now about

Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.client.transport.TransportClient.<init>(Lorg/elasticsearch/common/settings/Settings;Lorg/elasticsearch/common/settings/Settings;Ljava/util/Collection;Lorg/elasticsearch/client/transport/TransportClient$HostFailureListener;)V
	at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:92)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:81)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:71)
	at com.eab.certified.App.testPutAndGet(App.java:94)
	at com.eab.certified.App.main(App.java:58)

This is happening right at the same line of code:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200));

You are missing dependencies or you imported wrong dependencies or you are mixing multiple versions.

Ok, now I think I have it. I might suggest that the demo example include a bit more details. I had stitched together things from several different places, and what finally worked was having the following settings in my pom. I'm in business finally!

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>
		<elasticsearch.version>5.0.2</elasticsearch.version>
	</properties>

	<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-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-to-slf4j</artifactId>
			<version>2.6.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.21</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.7.21</version>
		</dependency>
		<!-- Elastic Search REST CLient -->
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>rest</artifactId>
			<version>5.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>transport</artifactId>
			<version>5.0.2</version>
		</dependency>
	</dependencies>

I am getting below error can you pls here, Elastic search jar Version used 5.1.1

SEVERE: Servlet.service() for servlet [issuetracker] in context with path [/issuetracker] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.elasticsearch.transport.client.PreBuiltTransportClient.addPlugins(Ljava/util/Collection;Ljava/util/Collection;)Ljava/util/Collection;] with root cause
java.lang.NoSuchMethodError: org.elasticsearch.transport.client.PreBuiltTransportClient.addPlugins(Ljava/util/Collection;Ljava/util/Collection;)Ljava/util/Collection;
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:92)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:81)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:71)

1 Like