I am using mvn package to build a jar and want to run the main function in the jar. mvn package is successful, but when I run java -classpath ./target/socket-server-1.0-SNAPSHOT.jar cn.zmhappy.es.Demo, something bad happened as follow:
Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/TransportAddress
at cn.zmhappy.es.Demo.main(Server.java:52)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.transport.TransportAddress
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
I try to search in ~/.m2/repository/org/elasticsearch/, but I can't find common under this directory. Besides, the application can run successfully locally. I wonder how to deal with this problem.
Elasticsearch version (bin/elasticsearch --version): 5.3.0 JVM version (java -version): 1.8.0_144
When you run java -classpath ./target/socket-server-1.0-SNAPSHOT.jar you are just passing your own code but you are not adding on the classpath any of the dependencies (and sub dependencies).
You need to add them all or create a flatjar which contains all that.
Thanks for your help! I follow your advice and add plugin in my pom.xml, but now there is another problem as follow:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.elasticsearch.common.logging.DeprecationLogger.<clinit>(DeprecationLogger.java:138)
at org.elasticsearch.common.xcontent.support.AbstractXContentParser.<init>(AbstractXContentParser.java:57)
at org.elasticsearch.common.xcontent.json.JsonXContentParser.<init>(JsonXContentParser.java:44)
at org.elasticsearch.common.xcontent.json.JsonXContent.createParser(JsonXContent.java:103)
at org.elasticsearch.common.settings.Setting.parseableStringToList(Setting.java:832)
at org.elasticsearch.common.settings.Setting.lambda$listSetting$27(Setting.java:786)
at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:791)
at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:786)
at org.elasticsearch.common.network.NetworkService.<clinit>(NetworkService.java:50)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:98)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:126)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:268)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:125)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:111)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:101)
at cn.zmhappy.es.Demo.main(CheckFunc.java:37)
Caused by: java.lang.IllegalStateException: Error finding the build shortHash. Stopping Elasticsearch now so it doesn't run in subtly broken ways. This is likely a build bug.
at org.elasticsearch.Build.<clinit>(Build.java:62)
... 16 more
I have no idea about this. Can you give me some help? Thanks!
My demo is totally the same as that in elasticsearch doc. It is just a simple demo.
I finally solve this problem by changing my dependency version from 5.3.0 to 5.5.2 as follow:
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.