0

Recently I was converting a spring boot(2.1.3 version) dummy project into a spring boot muti module project on java 11. To make it work I have to explicitly add --add-modules=java.instrument as run time vm argument. Please find the related so question here Spring boot multi module project with java 11 throws cannot access class org.springframework.cglib.core.ReflectUtils

Then I checked the default modules of my installed jdk using —list-modules and I can see that java.instrument in that. I was wondering why I need to add java.instrument explicitly?

I read it in https://openjdk.java.net/jeps/261 that

“It is occasionally necessary to add modules to the default root set in order to ensure that specific platform, library, or service-provider modules will be present in the resulting module graph. In any phase the option --add-modules (,) “* but I did not understand it completely.

Can some one with a spring/ java module system expertise explain it why it’s necessary to add explicitly some module even though it was implicitly there?

[updated with module-info.java and exception] This was the module-info.java of the springboot module

open module myModule {
    requires spring.boot.autoconfigure;
    requires spring.context;
    requires spring.boot;
    requires spring.web;
    requires org.jsoup;
}

This was the exception which I can see if I explicitly does not add --add-module java.instrument ( or requires java.instruement)

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.context@5.1.5.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.1.5.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at legalAppFetchModule/run.App.main(App.java:11) ~[classes/:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:415) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181) ~[spring-boot-2.1.3.RELEASE.jar:na]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.3.RELEASE.jar:na]
    ... 8 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:921) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:423) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:928) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:455) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at spring.boot@2.1.3.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:106) ~[spring-boot-2.1.3.RELEASE.jar:na]
    ... 13 common frames omitted
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during start
    at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    ... 21 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:921) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:831) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    ... 21 common frames omitted
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Error starting the loader
    at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    ... 29 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Error starting the loader
    at org.apache.catalina.loader.WebappLoader.startInternal(WebappLoader.java:413) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4980) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:902) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    ... 29 common frames omitted
Caused by: java.lang.NoClassDefFoundError: java/lang/instrument/ClassFileTransformer
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na]
    at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3192) ~[na:na]
    at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3198) ~[na:na]
    at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3198) ~[na:na]
    at java.base/java.lang.Class.getMethods(Class.java:1905) ~[na:na]
    at org.apache.tomcat.util.modeler.modules.MbeansDescriptorsIntrospectionSource.createManagedBean(MbeansDescriptorsIntrospectionSource.java:298) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.modeler.modules.MbeansDescriptorsIntrospectionSource.execute(MbeansDescriptorsIntrospectionSource.java:78) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.modeler.modules.MbeansDescriptorsIntrospectionSource.loadDescriptors(MbeansDescriptorsIntrospectionSource.java:71) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.modeler.Registry.load(Registry.java:598) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.modeler.Registry.findManagedBean(Registry.java:504) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.modeler.Registry.registerComponent(Registry.java:627) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.loader.WebappLoader.startInternal(WebappLoader.java:408) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    ... 38 common frames omitted
Caused by: java.lang.ClassNotFoundException: java.lang.instrument.ClassFileTransformer
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 51 common frames omitted
nantitv
  • 3,539
  • 4
  • 38
  • 61
  • How are you running the application? Afaik it should automatically add the module if you have it as a `requires` in your module-info – Jorn Vernee Apr 22 '19 at 10:05
  • my module-info.java was open module moduleName { requires spring.boot.autoconfigure; requires spring.context; requires spring.boot; requires spring.web; } – nantitv Apr 22 '19 at 10:51
  • I was running the spring-boot using spring toolsuite IDE – nantitv Apr 22 '19 at 10:51
  • 2
    java.instrument is for tool agents. If you've specified -javaagent on the command line then the runtime will ensure that java.instrument is resolved at startup. If you are saying that a Spring module has references to classes in the java.instrument then it should `requires java.instrument`. Maybe the Spring component is an automatic module, with no module declaration? – Alan Bateman Apr 22 '19 at 15:00
  • Hi @AlanBateman Thanks for having a look. The module had a module-info.java. I update the module-info.java and the exception. As you suggested requires java.instrument solves the problem. But I am wondering since the particular module is already there by default why an explicit requires/add-module is need? (plus this issue happens only in spring-multi module scenario? – nantitv Apr 22 '19 at 16:51
  • 1
    I suspect you are using an automatic module so there is no `requires java.instrument`. This is why you need to help out by specifying --add-modules or maybe an explicit module helps you by requiring java.instrument. In any case, the java.instrument is for agents so a bit strange to see runtime or non-agent code making use of types in that module. – Alan Bateman Apr 23 '19 at 10:05
  • @AlanBateman Thanks. I think I am using a automatic module because I put the spring jars on the module path – nantitv Apr 23 '19 at 19:29

0 Answers0