Run a Java class from Spring startup
<p>I am using Java8 and Spring4.3.1.</p>

<p>I have a Java/Spring application hosting RESTfult services accessed by browser and mobile app clients. Second, I have written a Chat Server that listens for events (<code>socket.io</code>) from the clients. This Chat Server is running from the classes <code>main</code> method.</p>

<p>The Chat Server class has a <code>main</code> method that I want to run, and allow to listen for events when my Spring application starts.  Is this possible?</p>

<p>If I run the <code>main</code> myself, it works, but I want it to start up when I start my Wildfly server that loads the Spring application.</p>

<p>Or is there a better approach?  Should the Chat Server not be running from the <code>main</code> method?</p>

<p>I have the following code:</p>

<pre><code>package com.jobs.spring.configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(servletContext);
        Dynamic dynamic = servletContext.addServlet("rest", new DispatcherServlet(ctx));
        dynamic.addMapping("/*");
        dynamic.setLoadOnStartup(1);

        try {
            com.jobs.spring.chat.Server chatServer = new com.jobs.spring.chat.Server();
            chatServer.run(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>and </p>

<pre><code>public class Server implements CommandLineRunner {

    private static final String SERVER = "localhost";
    private static final Integer PORT = 3700;

    @Override
    public void run(String... args) throws Exception {
        main(args);
    }

    public static void main(String[] args) {
...
</code></pre>

<p>and get the following error:</p>

<pre><code>18:47:08,142 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 66) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./jbosswildfly: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./jbosswildfly: java.lang.NoClassDefFoundError: Failed to link com/jobs/spring/chat/Server (Module "deployment.jbosswildfly.war:main" from Service Module Loader): org/springframework/boot/CommandLineRunner

Caused by: java.lang.NoClassDefFoundError: Failed to link com/jobs/spring/chat/Server (Module "deployment.jbosswildfly.war:main" from Service Module Loader): org/springframework/boot/CommandLineRunner
</code></pre>