3

I want to develop a modular app that uses a MySql db, Java 11 and spring boot. The app works perfectly well until I add the module descriptors; so I can only guess I am missing something in the module-info.java, but I can't figure out what it is. I appreciate any help. Here is my code

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/modules?serverTimezone=UTC
    username: modulesAdmin
    password: modules
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true
    properties:
      hibernate.format_sql: true

module-info.java

    requires java.persistence;
    requires spring.context;
    requires spring.beans;
    requires spring.data.jpa;
    requires spring.data.commons;
    requires spring.web;
    requires spring.boot;
    requires spring.boot.autoconfigure;

    opens com.jamsws.demojpa to spring.core;

    exports com.jamsws.demojpa to spring.beans, spring.context;
    exports com.jamsws.demojpa.resource to spring.beans;

User.java

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String teamName;
    private Integer salary;

    public User() {
    }

    public User(String name, String teamName, Integer salary) {
        this.name = name;
        this.teamName = teamName;
        this.salary = salary;
    }

    // getters and setters
}

UsersRepository.java

@Repository
public interface UsersRepository extends JpaRepository<User, Integer> {
}

UserResources.java

@RestController
@RequestMapping(value = "rest/users")
public class UserResources {

    @Autowired
    private UsersRepository usersRepository;

    @PostMapping(value = "/load")
    public List<User> persist(@RequestBody final User user) {
        usersRepository.save(user);
        return usersRepository.findAll();
    }
}

DemojpaApplication.java

@SpringBootApplication
@EntityScan(basePackages = "com.jamsws.demojpa.model")
@EnableJpaRepositories(basePackages = "com.jamsws.demojpa.repository")
public class DemojpaApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemojpaApplication.class, args);

    }

}

Error trace

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to stop component [WebappLoader[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
    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.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:982) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.StandardService.stopInternal(StandardService.java:473) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:992) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.startup.Tomcat.stop(Tomcat.java:478) ~[tomcat-embed-core-9.0.36.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:273) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:331) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:148) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at demojpa/com.jamsws.demojpa.DemojpaApplication.main(DemojpaApplication.java:15) ~[classes/:na]
Caused by: org.apache.catalina.LifecycleException: Failed to stop component [WebappLoader[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:267) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5431) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
    ... 23 common frames omitted
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166) ~[na:na]
    at java.base/java.lang.Class.getMethodsRecursive(Class.java:3307) ~[na:na]
    at java.base/java.lang.Class.getMethod0(Class.java:3293) ~[na:na]
    at java.base/java.lang.Class.getMethod(Class.java:2106) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc(WebappClassLoaderBase.java:1697) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.loader.WebappClassLoaderBase.clearReferences(WebappClassLoaderBase.java:1619) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.loader.WebappClassLoaderBase.stop(WebappClassLoaderBase.java:1555) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.loader.WebappLoader.stopInternal(WebappLoader.java:449) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    ... 31 common frames omitted
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:72) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188) ~[tomcat-embed-core-9.0.36.jar:na]
    ... 41 common frames omitted

2020-07-19 19:51:17.437 ERROR 10199 --- [           main] org.apache.catalina.core.ContainerBase   : A child container failed during stop

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during stop
    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.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:982) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.StandardService.stopInternal(StandardService.java:473) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:992) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.startup.Tomcat.stop(Tomcat.java:478) ~[tomcat-embed-core-9.0.36.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:273) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:331) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:148) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at spring.boot@2.3.1.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:na]
    at demojpa/com.jamsws.demojpa.DemojpaApplication.main(DemojpaApplication.java:15) ~[classes/:na]
Caused by: org.apache.catalina.LifecycleException: A child container failed during stop
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:990) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.tomcat.embed.core@9.0.36/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
    ... 16 common frames omitted

2020-07-19 19:51:17.441  INFO 10199 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-19 19:51:17.469 ERROR 10199 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field usersRepository in com.jamsws.demojpa.resource.UserResources required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

EDIT: Following @Naman suggestion I added a requires java.sql; to the module descriptor file which helped but the issue about "No bean named 'entityManagerFactory' available" is still there

2020-07-20 06:57:54.679  INFO 21643 --- [           main] com.jamsws.demojpa.DemojpaApplication    : Starting DemojpaApplication on anderson-ubuntu with PID 21643 (/home/anderson/Documents/JavaApps/PracticeProjects/demojpa/target/classes started by anderson in /home/anderson/Documents/JavaApps/PracticeProjects/demojpa)
2020-07-20 06:57:54.681  INFO 21643 --- [           main] com.jamsws.demojpa.DemojpaApplication    : No active profile set, falling back to default profiles: default
2020-07-20 06:57:54.973  INFO 21643 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-07-20 06:57:54.996  INFO 21643 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 1 JPA repository interfaces.
2020-07-20 06:57:55.272  INFO 21643 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-07-20 06:57:55.276  INFO 21643 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-20 06:57:55.276  INFO 21643 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-20 06:57:55.295  INFO 21643 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-20 06:57:55.295  INFO 21643 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 579 ms
2020-07-20 06:57:55.327  WARN 21643 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userResources': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepository' defined in com.jamsws.demojpa.repository.UsersRepository defined in @EnableJpaRepositories declared on DemojpaApplication: Cannot create inner bean '(inner bean)#5c080ef3' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5c080ef3': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-07-20 06:57:55.328  INFO 21643 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-07-20 06:57:55.338  INFO 21643 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-20 06:57:55.370 ERROR 21643 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field usersRepository in com.jamsws.demojpa.resource.UserResources required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.


Process finished with exit code 1

Anderson
  • 31
  • 4
  • 2
    HI, You have pasted code but forgot to paste the error stack trace. It would be good if you can upload this as a zip so that I can have a look. @EnableJpaRepositories(basePackages = "com.jamsws.demojpa.repository") Above line is not needed if your JPA entity is within subpackage of Main class. – SauriBabu Jul 20 '20 at 02:11
  • Hi @SauriBabu, I edited the post to include the stack trace; silly mistake. You can find the project at (https://1drv.ms/u/s!Akrt4fZYuj40k4gn6x0ezqFlse7ODQ?e=fA7tYP) Following Naman suggestion I added a requires java.sql to the module descriptor and it helped with that part of the trace but the error about "No bean named 'entityManagerFactory' available" is still there. Again, any suggestion is appreciated – Anderson Jul 20 '20 at 11:20
  • @Anderson for the update, did you take a look at [this question](https://stackoverflow.com/questions/48416927/spring-boot-required-a-bean-named-entitymanagerfactory-that-could-not-be-foun/54663039)? or tried executing the app with `--debug`? – Naman Jul 20 '20 at 11:37
  • Your module list seems too short, this article about migrating a Spring Boot application to Java 9 lists quite a few more: https://blog.frankel.ch/migrating-to-java-9/2/ . – Gimby Jul 20 '20 at 11:59
  • @Naman I did both and tried creating the entityManagerFactory bean but after adjusting all required dependencies I ended up with `Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set` which I tried to set but still get the same error. Also I noticed `Could not obtain connection to query metadata : The application must supply JDBC connections` but again if I delete the module-info.java the app works well – Anderson Jul 20 '20 at 13:44
  • Hi @Gimby, interesting article, however, I tried all suggestions and still have the same issue that could not find 'entityManagerFactory' bean – Anderson Jul 20 '20 at 13:55
  • 1
    Not entirely unexpected for the brave but unofficial efforts of a third party developer who just kept hacking until things did not break anymore. I don't have the idea that Spring in its current state supports the Java 9 module system as of yet. If this is true... I would not try to do a modularised application with Spring (Boot) just yet. https://github.com/spring-projects/spring-boot/issues/20903 – Gimby Jul 20 '20 at 15:49

2 Answers2

1

The primary cause of the exception seems to be

Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException

At the same time, the mention in your question that the "app works perfectly well until I add the module descriptors", seems to indicate that prior to introducing the module-info.java, your application must have been using the classpath, where it was able to find the class mentioned above through transitive dependencies.

1Another aspect would be, that your application doesn't directly need this class, else you would have faced a compile-time error stating that the imports are unrecognized.

This boils down to the module dependencies that you've mentioned in your application's descriptor, so one or more of them requires java.sql.SQLException now to be present on the modulepath and has itself missed specifying that explicitly (quite possible in the case of automatic modules). You would need to identify which module(s) amongst those required by you could need the specific class for a long term resolution.


1One way would be then to say, report it to the library/module owner and ask them to define an explicit dependency. This might require you to wait for them to be modular themself and then make use of the upgraded version. In such a case, you can until there upgrade add an argument to your application

--add-modules=java.sql 

Another could be that your application itself at runtime needs the java.sql module and that the libraries just make use of it when declared by you explicitly. In this case, the solution would be to add the following to your module-info.java file:

requires java.sql
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Hi @Naman, I followed your suggestion and added `required java.sql` and now I don't have the `java.lang.NoClassDefFoundError: java/sql/SQLException` however, the application still fails to start with `No bean named 'entityManagerFactory' available`. I have edited the question to show the error – Anderson Jul 20 '20 at 11:32
0

I have gone through your stack-trace. Can you try below steps

  1. Remove @Repository annotation from UserRepository.java
  2. Remove @EnableJpaRepositories from DemojpaApplication only if UserRepository.java is in sub package of main DemojpaApplication.java
SauriBabu
  • 414
  • 6
  • 15
  • I have tried that, but now it complains about not finding a bean of type UserRepository; that is because UserResources.java is autowiring the UsersRepository – Anderson Jul 22 '20 at 12:19
  • I have given you a suggestion after trying at my side. 1) https://spring.io/guides/gs/accessing-data-jpa/ as you can see here also @Repository is not needed at data jpa interface. 2) https://github.com/spring-guides/gs-accessing-data-mysql/issues/10 this points to bad maven repository as a reason. If still issue persists, enable logs in debug mode, and observe it carefully, you will find some pointers. Hope it helps. – SauriBabu Jul 23 '20 at 02:00