1

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?

Playdome.io
  • 3,137
  • 2
  • 17
  • 34
  • 1
    Could we see the code that causes the error? Just enough to reproduce the issue. – Ben N May 04 '15 at 13:13
  • 1
    Why are you tagging this with Maven? Are you using Maven? If so it helps to post the part of the pom where you declare your dependencies as this is apparently a problem of a dependency missing at runtime. PS: I would drop the eclipse tag; the fact that you use Eclipse has little to nothing to do with the question you ask. – Gimby May 04 '15 at 13:18
  • Yes i am using maven. Also I updated my answer. – Playdome.io May 04 '15 at 13:19
  • 1
    When does that happen? While running unit tests? When you create a JAR and try to run it? When you execute the code inside of Eclipse? – Aaron Digulla May 04 '15 at 13:21
  • It happens when i create a jar file and try to run it. – Playdome.io May 04 '15 at 13:22

4 Answers4

3

Evidently a library is missing. An internet search will yield

<dependency>
    <groupId>com.github.nkzawa</groupId>
    <artifactId>engine.io-client</artifactId>
    <version>0.4.1</version>
</dependency>

Then check for the latest version here. Should accord a little with your other dependency to nkzawa.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

When I look at the pom file I see a number of dependencies that this artifact has. Just referring to this pom will make maven download those dependencies, but they won't be included in your build unless you're building a jar-with-dependencies.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
1

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?

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • By the way i used IntelliJ to build the JAR file it took like 5 mins. I found eclipse a pain to use. Even after you helped to clear out a few things. Thanks for the help again! :) – Playdome.io May 05 '15 at 10:14
1

I was using socket-io following version :

<dependency>
    <groupId>io.socket</groupId>
    <artifactId>socket.io-client</artifactId>
    <version>0.8.2</version>
</dependency>

I had to add the following dependencies to the runtime to get this working.

<dependency>
    <groupId>io.socket</groupId>
    <artifactId>engine.io-client</artifactId>
    <version>0.8.2</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp-ws</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>com.squareup.okio</groupId>
    <artifactId>okio</artifactId>
    <version>1.11.0</version>
</dependency>

Refer to the pom.xml files to figure out the dependencies :

maheeka
  • 1,983
  • 1
  • 17
  • 25
  • Do you know if there is a way to bake all of those sub-dependencies automatically into our jar file? – garzj Mar 24 '21 at 10:53
  • Do you mean bundle it to your application? – maheeka Mar 25 '21 at 06:03
  • Yeah, shouldn't maven automatically include dependencies of dependencies? – garzj Mar 25 '21 at 09:37
  • Its not automatically done, but if you want to create a fat file or uber jar with all dependencies you can configure maven to do that. – maheeka Mar 28 '21 at 04:31
  • That's exactly what I'd want. Do you know how I could achieve this? I already have a maven project using the maven-shade-plugin, but it's just including socket.io-client-java in this case, ignoring its sub-dependencies. – garzj Mar 30 '21 at 19:31