Azarus Azarus - 19 days ago 7
Java Question

Java Socket.IO missing dependency NoClassDefFoundError

Hi guys im pretty new to java. Coming from .NET, C++. I am trying to get my head over with an error that is killing me since days and i am unable to fix it.

I get this error when i try to run my .jar file

java.lang.NoClassDefFoundError: com/github/nkzawa/emitter/Emitter$Listener


I have no clue what else it needs, or why its not loading the engine.io-client.

Any help appreciated.

Edit:

Crashing code part:

socket = IO.socket("http://blah.com:1234");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener()
{

// ...

});


pom.xml

<dependency>
<groupId>com.github.nkzawa</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.5.0</version>
</dependency>


Is there any other way to make sure that these dependencies are shipped with the final jar file so i don't have dependency issues on the end-user's machine?

Answer

When you compile C++ code, you have to give the compiler a list of (shared) libraries which your code depends on. In Java, you can specify a list of JARs which are added to the "classpath". Unlike C++, Java allows to change the classpath after the code has been compiled. So you can add additional elements or remove existing ones. In C++, you usually can replace the DLL file on disk but you need special tricks to get your code to load more DLLs or forget about ones that it was linked against.

This causes all kind of weird problems like the one which you experience right now: Eclipse builds the classpath for you, everything looks fine. You export the project as an executable JAR and suddenly, you have to do a lot of tedious work just to replicate a click of a button in the IDE.

The most simple way to get the classpath is to start your program in the IDE once, switch to the Debug perspective and check the properties of the process which Eclipse created. It will display the Java command line which it built. It's not a pretty sight.

Copy the whole thing into a .cmd file and use that to start your application.

You can also use Maven to create executable JARs with several options how to handle dependencies: How can I create an executable jar with dependencies using Maven?

Comments