0

I use Swarm 2017.3.X and 2017.5.0. When I add a new class in a new package I get NoClassDefFoundError caused by java.lang.ClassNotFoundException on runtime. Why?

org.jboss.resteasy.spi.UnhandledException: java.lang.NoClassDefFoundError
...
Caused by: java.lang.NoClassDefFoundError: com/example/myapp/newpackage/NewClass
at com.example.myapp.MyBean.getSomething(MyBean.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:402)
... 45 more
Caused by: java.lang.ClassNotFoundException: com.example.myapp.newpackage.NewClass from [Module "deployment.198f19d8-46c4-4bf5-96fd-101dc0f57abd.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
... 57 more

Swarm finds all the Java classes in com.example.myapp (where Main.java is) but not the classes in com.example.myapp.newpackage (where NewClass.java is).

Andreas Panagiotidis
  • 2,763
  • 35
  • 32

1 Answers1

0

In Swarm you have to add each package in Main class:

    deployment.addPackage(Main.class.getPackage());
    deployment.addPackage(NewClass.class.getPackage()); //new
    deployment.addAllDependencies();

Then it works!

Andreas Panagiotidis
  • 2,763
  • 35
  • 32
  • 1
    Note that in the vast majority of cases, you don't really have to use a custom main method and manually construct a deployment. If you're using the main method for configuring Swarm, then `project-defaults.yml` is probably a better choice, and even if you really need to use custom main method, you don't always have to construct the deployment manually, as the default deployment will probably be exactly what you need. – Ladicek May 09 '17 at 11:09
  • Thanks, I looked into that. – Andreas Panagiotidis May 09 '17 at 11:22