Problem:
When trying out the SparkJava demo for websockets (spark-websockets, "Using WebSockets and Spark to create a real-time chat app"), I get the following compilation error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at spark.Service.<clinit>(Service.java:56)
at spark.Spark$SingletonHolder.<clinit>(Spark.java:51)
at spark.Spark.getInstance(Spark.java:55)
at spark.Spark.<clinit>(Spark.java:61)
at Chat.main(Chat.java:19)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
Workaround:
The following workaround worked as a quick way to try out the demo code:- Open the project's pom.xml file
- Add the following dependency:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> <scope>compile</scope> </dependency>
If this worked, the project should now compile properly.
Notes:
The above workaround is one of many solutions that can work towards a successful compile. Others include adding a JAR file for a logger, for example. This solution was chosen as it is the quickest for a beginner to do while learning how to use SparkJava or while learning programming in Java.This how-to was verified to work in revision 8cc09e8cea9257a0df3449106a57ad0467faf39b (Sep 4, 2017) of the spark-websockets project. Your results may vary for other revisions.