Access Elastic Search Using Its Java Api's - java

I am trying to follow the Documentation of Elastic Search to get and post data from it using its java Api's. But i am constantly getting error.
This is the code i have tried as of now.
Settings settings = Settings.builder().put("twitter", "tweet").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
GetResponse response = client.prepareGet("twitter", "tweet", "1").get();
The stack trace
Exception in thread "main" java.lang.NoClassDefFoundError:
com/google/common/collect/ImmutableSortedMap
at com.c1x.entity.Test.main(Test.java:20)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableSortedMap
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Please Help , i am new to Elastic search , the data is present in elastic search as i can see it when i use dev tools in kibana
after adding google commons
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/SetOnce
at org.elasticsearch.common.settings.Settings$Builder.<init>(Settings.java:676)
at org.elasticsearch.common.settings.Settings$Builder.<clinit>(Settings.java:671)
at org.elasticsearch.common.settings.Settings.<clinit>(Settings.java:84)
at com.c1x.entity.Test.main(Test.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.util.SetOnce
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
After adding lucene jar
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
at org.elasticsearch.common.Strings.<clinit>(Strings.java:459)
at org.elasticsearch.common.settings.Settings.<init>(Settings.java:90)
at org.elasticsearch.common.settings.Settings$Builder.build(Settings.java:1215)
at com.c1x.entity.Test.main(Test.java:21)
Another error
Exception in thread "main" java.lang.VerifyError: class org.elasticsearch.transport.Netty3Plugin overrides final method onModule.(Lorg/elasticsearch/common/network/NetworkModule;)V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<clinit>(PreBuiltTransportClient.java:84)
at com.c1x.entity.Test.main(Test.java:22)

You miss the google-collections.jar in your classpath. Add it and the error will go away.
If you are using maven as dependency Management tool, add the follwing lines to your pom.xml:
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
Next jar is missing add lucene-core-<version>.jar
maven dependency is:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.5.1</version>
</dependency>

Related

Hbase-Spark :Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/Logging

I am trying to get data from hbase database (1.2.5) using Spark 2.0.2 with the code in this page :
https://github.com/apache/hbase/blob/master/hbase-spark/src/main/java/org/apache/hadoop/hbase/spark/example/hbasecontext/JavaHBaseBulkGetExample.java
But I get this error :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/spark/Logging at java.lang.ClassLoader.defineClass1(Native
Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at
java.net.URLClassLoader.access$100(URLClassLoader.java:73) at
java.net.URLClassLoader$1.run(URLClassLoader.java:368) at
java.net.URLClassLoader$1.run(URLClassLoader.java:362) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:361) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
com.cloudera.spark.hbase.JavaHBaseContext.(JavaHBaseContext.scala:23)
at org.hipi.examples.Test.main(Test.java:71) 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.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at
org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) Caused
by: java.lang.ClassNotFoundException: org.apache.spark.Logging at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 22 more
I have seen on the internet that Logging class exist only in spark 1.5.2 and lower versions.
Is there is any solution to make that work even with Spark 2.0.2 ?
There are few BUGs related with this:
https://issues.apache.org/jira/browse/SPARK-13928
https://issues.apache.org/jira/browse/HBASE-16428

CXF- client giving "java.lang.VerifyError: Cannot inherit from final class"

Hi i am using the following code to call a cxf service and getting the error. how to resolve this?
WebClient client = WebClient.create("http://localhost:8080/weath/api/v1/cities/cities", "test", "murali", null);
The above code is giving this error:-
Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.ymd.CxfClient.main(CxfClient.java:29)
All the jars that i have in my classpath are:-
cxf-rt-rs-client-3.1.7.jar
cxf-rt-transports-http-hc-3.1.7.jar
cxf-bundle-jaxrs-2.1.2.jar
cxf-rt-frontend-jaxrs-3.1.7.jar
cxf-core-3.1.7.jar
cxf-rt-transports-http-3.1.7.jar
javax.persistence-2.1.1.jar
-cxf-api-2.7.18.jar
javax.ws.rs-api-2.0.jar
I resolved my problem. it is because of version conflict. When i use maven for getting dependencies everything got resolved.

I dont know why i am getting this error with junit [duplicate]

This question already has answers here:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
(20 answers)
Closed 7 years ago.
I have the following folder structure
base directory = "."
build directory (for classes) = "./build/classes"
build directory (for test classes) = "./build/test/classes"
source = "./src"
test = "./test"
test_libraries = "./test_libraries"
everything works fine When I am running the test files with the command
java -cp /usr/share/java/junit4-4.11.jar:build/classes:build/test/classes/ org.junit.runner.JUnitCore myclass.MyClassTest
but when I change the location of the juint file to test_libraries/junit4-4.44.jar
and I run the command
java -cp ./test_libraries/junit4-4.11.jar:build/classes:build/test/classes/ org.junit.runner.JUnitCore myclass.MyClassTest
I get the following errors
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
You need Hamcrest on your classpath.
You are missing Hamcrest Jar
http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
JUnit depends on Hamcrest for a lot of methods.

Want to run optaplanner examples on my laptop but it throws an exception

When i run command ./runExamples.sh on terminal it throws an exception. I have checked that Java is installed and class path is already set. How to resolve this?
Starting examples app with java from environment variable PATH...
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.optaplanner.examples.common.app.CommonApp.<clinit>(CommonApp.java:36)
at org.optaplanner.examples.app.OptaPlannerExamplesApp.main(OptaPlannerExamplesApp.java:72)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
You miss the slf4j.jar in your classpath. download and add it.

Spring and Quartz error

I am trying the example from following site
http://www.mkyong.com/spring/spring-quartz-scheduler-example/
I have added junit to fix build error but getting the following error...any help is appreciated
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
at com.mkyong.common.App.main(App.java:7)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlApplicationContext
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Add spring-context-support.jar and spring-context.jar in your application path and then run application

Categories

Resources