2

I'm using the Java API. I tried the following:

Client client = TransportClient.builder().build() 
 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200));

But it gave me the following error:

Exception in thread "main" java.lang.NoClassDefFoundError:       com/google/common/collect/ImmutableMap
at org.elasticsearch.client.transport.TransportClient$Builder.<init>(TransportClient.java:84)
at org.elasticsearch.client.transport.TransportClient.builder(TransportClient.java:76)
at tools.Tools.Searcher(Tools.java:195)
at tools.Tools.main(Tools.java:60)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 4 more
Java Result: 1

I am using Elasticsearch 2.0.0 and it is running on port 9200

What is causing that error and how can I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
hello_its_me
  • 743
  • 2
  • 19
  • 52
  • 2
    Supporting jar(google-collections.jar) is not available in the buildpath it seems. – Karthick Nov 24 '15 at 09:01
  • @Karthick Okay, I downloaded the google-collections-1.0.jar file, and imported `import com.google.common.*;`, however I am still getting an error: `Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of(Ljava/lang/Obje...` – hello_its_me Nov 24 '15 at 10:01
  • It is great to use some dependency management tool like Maven to manage dependencies rather than manually downloading jar files. Maven will automatically download all the dependencies. You just need to add the dependency like org.elasticsearch elasticsearch 2.0.0 – Shivanand Pawar Nov 24 '15 at 11:13
  • @ShivanandPawar I see. I wanted to use that solution originally, but I'm working with a java application. How can I add this dependency with the java application? Should I create a new Maven application, or just add a pom.xml file? – hello_its_me Nov 24 '15 at 11:16
  • You should convert your project into maven project. If you are using eclipse then check this [link](http://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/) or if you are using Intellij then check this [link](http://stackoverflow.com/questions/7642456/intellij-convert-a-java-project-module-into-a-maven-project-module) to convert into a maven project. – Shivanand Pawar Nov 24 '15 at 11:22
  • @ShivanandPawar I also wanna mention that I am using the elasticsearch-2.0.0.jar file. But it's still giving me these errors. I'm now going to convert my project to a maven project, see if it works. – hello_its_me Nov 24 '15 at 11:24
  • I'm also using Netbeans @ShivanandPawar – hello_its_me Nov 24 '15 at 11:25
  • Yeah apart from elasticsearch-2.0.0.jar,you need another few jars like lucene-core, lucene.queries, sparial4j, guava, jackson etc.The list is a little long and is not fitting in the comment box The class which you are getting error is in com.google.guava:guava:18.0. Never used Netbeans. So do not know how to convert to maven in NetBeans. But should not be a very difficult thing. – Shivanand Pawar Nov 24 '15 at 11:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96021/discussion-between-issy-and-shivanand-pawar). – hello_its_me Nov 24 '15 at 11:43

2 Answers2

1

I have this problem too, but when i add the guava-18.0.jar into build path ,it works. Hope it works for you.

chou
  • 344
  • 3
  • 17
  • You didn't use Maven at all? – hello_its_me Nov 26 '15 at 09:56
  • After adding the guava jar, I got the error `Nov 26, 2015 12:00:44 PM org.elasticsearch.plugins.PluginsService INFO: [Kraken] loaded [], sites [] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/Version at org.elasticsearch.Version.(Version.java:44) at org.elasticsearch.client.transport.TransportClient$Builder.build(TransportClient.java:117)` – hello_its_me Nov 26 '15 at 10:00
  • that means you lack another jar, can you paste the details of the error messages? when i added the guava jar, it also had other problems ,but all NoClassDefFoundError, so i check what jar it miss time and time again, and add the missing jar into build path. well ,the jar i added as follows and it works: – chou Nov 27 '15 at 04:55
  • The jar i added as follows and it works: commons-codec-1.9.jar, commons-logging-1.2.jar, elasticsearch-2.0.0.jar, guava-18.0.jar, jackson*.jar(all of the jackson jar), joda*.jar, jsr166e-1.1.0.jar, lucene-core-5.2.1.jar, netty-3.1.0.5.Final.jar, sheild-2.0.0.jar, t-digest-3.0.jar. these jar can be found in lib directory of elasticsearch zip. if you feel trouble, you can add all the jar into build path – chou Nov 27 '15 at 05:01
  • I just kept adding the jars for each error I got til it worked. I ended up adding about 15 jar files. Thank you :) – hello_its_me Nov 27 '15 at 12:32
1

You can use Unirest API for Java. It is very easy to use.

http://unirest.io/java.html

Check it's documentation. I myself am a beginner and I found this very easy to implement.

Just add the following maven dependency in your pom file.

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.7</version>
</dependency>

And your java implementation would be like this:

HttpResponse<String> response = Unirest.get("http:localhost:9200").asString();
Amriteya
  • 1,118
  • 15
  • 37