4

I'm using JDK 11. My project started as a Spring Boot project with persistence, and I first got all the data classes wired up and covered in tests. Now I want to use JavaFX for GUI purposes, which meant starting to use the Java module system. The application (code skeleton shown below) starts up halfway but then breaks because of the following stack of exceptions:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties': Lookup method resolution failed

Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.orm.jpa.JpaProperties] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6c3708b3]

Caused by: java.lang.NoClassDefFoundError: javax/sql/DataSource

Caused by: java.lang.ClassNotFoundException: javax.sql.DataSource

The module info currently looks like this:

requires java.persistence;
requires javafx.controls;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.core;

exports mypackage.app;
opens mypackage.app;

And here is the (slightly shrinkwrapped) Java source of the main application:

package mypackage.app;

@SpringBootApplication
public class MyApplication extends Application {

    private static String[] arguments;

    private ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) {
        arguments = args;
        launch(args);
    }

    @Override
    public void init() throws Exception {
        applicationContext = SpringApplication.run(MyApplication.class, arguments);
    }

    @Override
    public void start(Stage stage) throws Exception {
        var label = new Label("xyz");
        var scene = new Scene(new StackPane(label), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void stop() {
        applicationContext.stop();
    }

}

Edit.

Adding a requires java.sql; to the module info leads to a different stack of exceptions, reproduced here:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed

javax.persistence.PersistenceException: Unable to resolve persistence unit root URL

java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist

Edit.

As indicated elsewhere, adding the java.xml.bind dependency addressed the persistence unit root URL issue, only to replace it with a new stack of exceptions:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed

java.lang.NoClassDefFoundError: net/bytebuddy/matcher/ElementMatchers

java.lang.ClassNotFoundException: net.bytebuddy.matcher.ElementMatchers
haupz
  • 226
  • 2
  • 5
  • The `javax.sql.DataSource` class is part of the `java.sql` module. Try adding a `requires` for that. – Stephen C Jul 28 '19 at 10:51
  • Thank you; I had actually tried that, and it led to a different stack of exceptions that mystified me as well at first glance, and I hadn't followed up on that in my thinking. I've updated the original question above. – haupz Jul 28 '19 at 18:42
  • That probably means you have a problem in the configs related to the entityManager bean / persistence unit root URL. – Stephen C Jul 28 '19 at 23:13
  • Yes it does, hence my asking. ;-) – haupz Aug 01 '19 at 18:14
  • And what are you going to do about it? Hint.... – Stephen C Aug 01 '19 at 22:53
  • I honestly believe the persistence unit root URL issue is only a surface issue; note that things used to be just fine before introducing the module system. As found elsewhere, adding the java.xml.bind requirement helped with the above issue, but now there's a new one. (Editing my post now to clarify that.) – haupz Aug 03 '19 at 15:54
  • That one is pretty obvious too. The JAR file for the net.bytebuddy dependency is missing at runtime. Check this: https://stackoverflow.com/questions/53306613/springboot-2-1-0-throws-classnotfoundexception-when-trying-to-integrate-database – Stephen C Aug 03 '19 at 16:03
  • Yeah, thanks; I had come across the SO question you mention as well, and my result is the same with or without an explicitly added ByteBuddy dependency, and with or without explicit runtime scoping. – haupz Aug 03 '19 at 16:51
  • 1
    ... however adding the `net.bytebuddy` requirement to the modules info resolves the issue. Thank you for your patience and support - just bear in mind that for labels such as "obvious", YMMV. ;-) – haupz Aug 03 '19 at 16:55

1 Answers1

5

The following module-info.java addresses the issue:

requires java.persistence;
requires java.sql;
requires java.xml.bind;
requires javafx.controls;
requires net.bytebuddy;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.core;
requires spring.data.jpa;

exports mypackage.app;
opens mypackage.app;

In a nutshell, the dependencies that had to be added were java.sql, spring.data.jpa, java.xml.bind, and net.bytebuddy. Moreover, the application package needed to be opened and exported by means of the two final lines.

haupz
  • 226
  • 2
  • 5