1

I'm having issues with finding resources since I start using java modules.

The same exact code: Thread.currentThread().contextClassLoader.getResource("db/migration/V1__init.sql")?.path behave different, with java-module it returns null, without it, it returns the correct path.

I guess I'm missing something about java-modules visibility.

I'm having problems with flyway loading migration files. I've made an example code in github

fritsMaister
  • 532
  • 1
  • 4
  • 18
  • 1
    Resources in named modules are encapsulated by default. The ClassLoader getResource methods can only locate resources in packages that are open to all modules. Is the V1__init.sql resource in your own module? If so, use Class.getResource instead. – Alan Bateman Nov 26 '19 at 08:53
  • 1
    When you need resources of your own module, you should not ask the thread's context class loader, a feature that only exists to allow setting it to a *different* class loader. – Holger Nov 26 '19 at 14:32
  • I moved the `V1__init.sql` file to `com\my\color\view\V1__init.sql` and I open this package `opens com.my.color.view;` With this configuration flyway can access the file when I run `gradle run` but it can't when I run it with intelij or with running the generated image after `gradle jlink` – fritsMaister Nov 26 '19 at 21:19
  • Just to emphasize the things already said, you should access the resource like, e.g. `ColorsListView.class.getResource("V1__init.sql")`, so that package and module are implicitly given. Then, you must use the returned `URL` as-is, instead of assuming that its `getPath` method returns a file. After packaging the code as jar or image, the resource is not a file anymore. – Holger Nov 27 '19 at 08:27

0 Answers0