title: How to fix, 'class path resource [...] cannot be opened because it doesn't exist' at runtime with Spring
body: <p>When trying to implement a simple websocket implementation into our Springboot application I keep running into an error, apologies as I have to obfuscate some of the non-problem related information. </p>
  
  <pre><code>Failed to load bean class: com.######.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
  </code></pre>
  
  <p>The problem happens at runtime when I try to deploy to debug and test but not during the Maven build.</p>
  
  <p>I have tried multiple example on the web for implementing websockets with Spring and JS/React using Stomp, SockJS and I always come up with some form of this same error message, where SwaggerConfig has a nested FileNotFoundException due to some class I need to create the websocket connection on my server side. Be aware that I am limited to Spring version <strong>4.0.9.RELEASE</strong> due to wider project dependencies so I can't use anything requiring newer deps.</p>
  
  <p>My following code uses the bare minimum example found at,
  <a href="https://www.javainuse.com/spring/boot-websocket" rel="nofollow noreferrer">https://www.javainuse.com/spring/boot-websocket</a></p>
  
  <p>My handler class:</p>
  
  <pre><code>import java.io.IOException;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.springframework.stereotype.Component;
  import org.springframework.web.socket.TextMessage;
  import org.springframework.web.socket.WebSocketSession;
  import org.springframework.web.socket.handler.TextWebSocketHandler;
  
  @Component
  public class SocketTextHandler extends TextWebSocketHandler {
  
      @Override
      public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException, JSONException {
  
          String payload = message.getPayload();
          JSONObject jsonObject = new JSONObject(payload);
          session.sendMessage(new TextMessage("Hi " + jsonObject.get("user") + " how may we help you?"));
      }
  
  }
  </code></pre>
  
  <p>My websocket-config class:</p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  import org.springframework.web.socket.config.annotation.EnableWebSocket;
  import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
  import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
  
  @Configuration
  @EnableWebSocket
  public class WebSocketConfig implements WebSocketConfigurer {
  
      public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
          registry.addHandler(new SocketTextHandler(), "/user");
      }
  
  }
  </code></pre>
  
  <p>My Pom.xml includes the following dependencies:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;spring.version&gt;4.0.9.RELEASE&lt;/spring.version&gt;
  &lt;/properties&gt;
  ...
  &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
  ...
  &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
          &lt;version&gt;1.1.12.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Finally my React app uses this component to simply display the data when/if it connects:</p>
  
  <pre><code>const WebSocketStream = () =&gt; {
      const [text, setText] = React.useState&lt;string&gt;("Hello!");
      const [connected, setConnected] = React.useState&lt;boolean&gt;(false);
      let ws:WebSocket;
  
      const connect = () =&gt; {
          ws = new WebSocket('ws://localhost:8080/app/user');
          ws.onmessage = (data) =&gt; {
              setText(data.data);
          }
          setConnected(true);
      };
  
      const disconnect = () =&gt; {
          if (ws != null) {
              ws.close();
          }
          setConnected(false);
          console.log("Websocket is in disconnected state");
      };
  
      const sendMessage = () =&gt; {
          const data = JSON.stringify({
              'user' : 'I was here'
          });
          ws.send(data);
      }
  
      return (
          &lt;WebSocketStreamWrapper&gt;
              {text + "\n"}
              {connected ? "Connected!" : "Not connected :("}
              &lt;Button onClick={connect}&gt;Connect&lt;/Button&gt;
              &lt;Button onClick={sendMessage}&gt;Send Message&lt;/Button&gt;
              &lt;Button onClick={disconnect}&gt;Disconnect&lt;/Button&gt;
          &lt;/WebSocketStreamWrapper&gt;
      );
  };
  
  export default WebSocketStream;
  </code></pre>
  
  <p>I would expect the server to at least get up and running so I can hopefully connect from the front end with a websocket. Here is the full error message,</p>
  
  <pre><code>16:16:05,380 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-4) Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:163) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:305) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:611) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [org.springframework-spring-web-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [org.springframework-spring-web-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [org.springframework-spring-web-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [rt.jar:1.8.0_202]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [rt.jar:1.8.0_202]
      at java.lang.Thread.run(Thread.java:748) [rt.jar:1.8.0_202]
  Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) [com.business.ws-viz-script-common-8.9.1-20190523.213339-28.jar:]
      at org.springframework.core.type.classreading.SimpleMetadataReader.&lt;init&gt;(SimpleMetadataReader.java:50) [com.business.ws-viz-script-common-8.9.1-20190523.213339-28.jar:]
      at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98) [com.business.ws-viz-script-common-8.9.1-20190523.213339-28.jar:]
      at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102) [com.business.ws-viz-script-common-8.9.1-20190523.213339-28.jar:]
      at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:93) [com.business.ws-viz-script-common-8.9.1-20190523.213339-28.jar:]
      at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:566) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:741) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:285) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:177) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:253) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:177) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:159) [org.springframework-spring-context-4.0.9.RELEASE.jar:4.0.9.RELEASE]
      ... 18 more
  
  16:16:05,389 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./app: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./app: Failed to start service
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [rt.jar:1.8.0_202]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [rt.jar:1.8.0_202]
      at java.lang.Thread.run(Thread.java:748) [rt.jar:1.8.0_202]
  Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
      ... 3 more
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:163)
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:305)
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:611)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193)
      ... 7 more
  Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
      at org.springframework.core.type.classreading.SimpleMetadataReader.&lt;init&gt;(SimpleMetadataReader.java:50)
      at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98)
      at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
      at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:93)
      at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:566)
      at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:741)
      at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:285)
      at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219)
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:177)
      at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:253)
      at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219)
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:177)
      at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:159)
      ... 18 more
  
  16:16:05,399 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 4) JBAS014613: Operation ("deploy") failed - address: ([("deployment" =&gt; "app-ear-10.0.0-SNAPSHOT.ear")]) - failure description: {"JBAS014671: Failed services" =&gt; {"jboss.undertow.deployment.default-server.default-host./app" =&gt; "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./app: Failed to start service
      Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist"}}
  16:16:05,401 ERROR [org.jboss.as.server] (management-handler-thread - 4) JBAS015870: Deploy of deployment "app-ear-10.0.0-SNAPSHOT.ear" was rolled back with the following failure message: 
  {"JBAS014671: Failed services" =&gt; {"jboss.undertow.deployment.default-server.default-host./app" =&gt; "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./app: Failed to start service
      Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.business.swagger.SwaggerConfig; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist
      Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/socket/handler/TextWebSocketHandler.class] cannot be opened because it does not exist"}}
  </code></pre>