Skip to content
Advertisement

How can I fix NoClassDefFoundError when using Spring framework’s WebSocketClient

I am writing a desktop Java application as a web service client. I want to use WebSocket to implement notification “callback” from the server.

I am using the Spring framework’s WebSocketStompClient. Below snippet shows how I initialize it:

JavaScript

It works perfectly if I run it in IntelliJ IDE, however, if I run by command line “java -cp my.jar MyPackage.MyMainClass”, it will fail with the following message:

JavaScript

The above was produced by Java SE 12.0.2. If I run it using Java SE 1.8, the error message will be:

JavaScript

Both java.lang.NoClassDefFoundError and getDeclaredMethods0(Native Method) suggsted that a native module (a DLL) is missing.

Below is my Gradle script:

JavaScript

I don’t think it was due to some missing JARs. I tried:

  • to add tyrus-standalone-client to the dependency list, or
  • to use StandardWebSocketClient instead of SockJsClient,

but the error remained.

I guess some native libraries are missing in my OS (Win10), but that can’t explain why I can run it in IntelliJ…

Could you let me know what is missing? or how can I fix this? Thanks!

Advertisement

Answer

Try to create a fat jar which contains all the dependencies and classes.

Update build.gradle script to this given below :

JavaScript

It will generate an executable fat jar in the build/libs of your root project which will include all the project files (this includes all of your dependencies, resource files, .class files and so on).

Note: For older versions of Gradle, use this configurations.compile.collect instead of configurations.runtimeClasspath.collect.

You just need to run the jar with this command : java -jar <project-version>.jar

This should solve your problem.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement