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?

9
  • 2
    Supporting jar(google-collections.jar) is not available in the buildpath it seems. Commented Nov 24, 2015 at 9: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... Commented Nov 24, 2015 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 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.0.0</version> </dependency> Commented Nov 24, 2015 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? Commented Nov 24, 2015 at 11:16
  • You should convert your project into maven project. If you are using eclipse then check this link or if you are using Intellij then check this link to convert into a maven project. Commented Nov 24, 2015 at 11:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You didn't use Maven at all?
After adding the guava jar, I got the error Nov 26, 2015 12:00:44 PM org.elasticsearch.plugins.PluginsService <init> INFO: [Kraken] loaded [], sites [] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/Version at org.elasticsearch.Version.<clinit>(Version.java:44) at org.elasticsearch.client.transport.TransportClient$Builder.build(TransportClient.java:117)
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:
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
I just kept adding the jars for each error I got til it worked. I ended up adding about 15 jar files. Thank you :)
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.