java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/TransportAddress

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

my pom.xml:

<dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
</dependencies>

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.

With Maven you can do the later like this:

And this:

Hope this helps

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!

No idea. Never saw that. Could you share your code?

BTW you should better use the Rest client as the Transport one will be deprecated at some point.

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:

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

I wonder if es is downward compatible? At present I didn't find the disadvantage.

Ha! You need to use the same version on both sides.

Rest Client is much more tolerant.

Thanks for your help, I will try to update my code by using Rest Client. Thanks again.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.