Can't access resource with Java 10

Answered

So I've got a small Javafx application that I wrote in Java 8 and converted to Java 10 just a few days ago.  When I build using Gradle and run from the command line, just using java -jar, it works fine.  When running from inside Intellij, it fails to load fxml files for dialogs.  These files are stored in the top level of the src/main/resources, and the location and the code that loads them hasn't changed.  I have added a module-info.java to the root of my Java source directory (src/main/java), and that resolves all the errors that Intellij gives me about having to require and export modules.  If I get rid of the modules-info.java, then the app works - but then Intellij complains about not having the appropriate requires and exports.

As you might guess, I am new to Java 10, so I'm definitely not an expert in the module system.  But it builds and works from the command-line, so it seems like this is an Intellij issue.  Any ideas about what I'm missing or doing wrong would be greatly appreciated.

Thanks

12 comments
Comment actions Permalink

Does the command line build use the same JDK and language levels as IntelliJ IDEA?

Changing the language level to 1.8 may help (note that it's done in multiple places, see http://stackoverflow.com/a/12900859/104891 for the screenshots).

0
Comment actions Permalink

Thanks for the quick response.

Yes, the command line uses the same Java 10 SDK as the project.  I can change the language level to 1.8, but I would like to use the new features.  The only problem I'm having is accessing the fxml files in the resource folder.  Surely that can be done with Java 10.

0
Comment actions Permalink

Please share a sample project to reproduce the issue.

0
Comment actions Permalink

https://github.com/hculpan/intellij-test

This is just a quick test project I threw together.  It's got just one class, Main, that just tries to load test.txt using ClassLoader.getSystemResource(), and outputs a text based on whether it finds it or not.  If I remove module-info.java, it finds the resource file.  If it's present, it doesn't.

0
Comment actions Permalink

The issue here is more complicated as it seems to be.

1. Gradle doesn't use Java 10 language levels by default, you have to define it per https://stackoverflow.com/questions/28450703/how-to-set-the-language-level-in-gradle-so-it-is-ide-agnostic.

2. Gradle doesn't use jigsaw module system by default, you have to adjust the build scripts to use it: https://guides.gradle.org/building-java-9-modules/.

3. Running the produced jar like `java -jar` also doesn't use the module system by default, you need to use -p and -m command line options like in this example https://carlfx.wordpress.com/2016/04/26/a-javafx-helloworld-using-java-9s-project-jigsaw-in-60-seconds/ .

4. Running from a jar appears to be different than running from classpath output directories, see the command line used by IntelliJ IDEA, for some reason -p and -m options do not work for resources.

5. There is an issue related to resource loading reported at https://youtrack.jetbrains.com/issue/IDEA-180549, however it's a bit different since your resource is in the root folder which appears to work in that example.

I'd suggest to not use the module-info.java at all. You will still be able to use Java 10 features except jigsaw.

0
Comment actions Permalink

The issue I run into if I don't use the modules-info.java is that Intellij will give me a bunch of errors, as per this discussion https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000414279-Module-Graph-Problem-with-2018-2.  The test project did not use any external libs, but my project does, and thus I get these errors.

My guess is that I'll just set the language level back to 8 and just not use any of the Java 9/10 language features.  It seems like it's an all-or-nothing proposition, and using modules seems more complex than I need.

Thanks.

0
Comment actions Permalink

Since classes and resources are placed into different directories by Gradle, it may be the cause of the problem (related: https://stackoverflow.com/a/49698440/104891). When they are in the jar, classes and resources appear in the same tree and it starts to work.

This comment fixed the issue for me in your sample project:

allprojects {
apply plugin: 'idea'
idea.module.outputDir file("out/production/classes")
}

With this change classes and resources appear in the same output directory which makes the module system happy and it starts to behave the same way as if the classes/resources were packaged in a jar.

See also https://github.com/gradle/gradle/issues/4733.

0
Comment actions Permalink

I'm sorry to post to this old thread.

I ran into the exactly same issue, and for me the best solution I think is to add "--patch-module {your-module-name}={path-to-your-project}/out/production/resources", because I didn't want to edit gradle file to make IDE behave correctly --- I'd rather expect the IDE itself would adjust to the project.   I want Gradle plugin for Intellij to add feature to automatically set --patch-module options when running Gradle project....

2
Comment actions Permalink

I have the same problem since switching from Java 8 to Java 11.
However, this is about a spring-boot application with a JFX integration.

If the application starts under maven with spring-boot: run or with java -jar spring-boot-jfx.jar it works great. But as soon as I start the application under IntelliJ, the .fxml and my messegae resources will not be found anymore.

My application consists of several modules and means that each module can contain a different .fxml file because the JavaFX application is modular. Under Java 8 no problem ...!

IntelliJ, spring-boot and Java11 is bad to handle.

Excuse my english.

0
Comment actions Permalink

Hello Steven,

What IDE version do you use? What error message do you get?

0
Comment actions Permalink

Hey,

There is no error except the FileNotFound Exception if various .fxml files are not found as soon as the Spring Boot application runs under the IDE. There are simply no resources found in the individual modules. (.fxml, /i18n/messages etc.)
Even in debug mode, I can not read the resources.

Example for .fxml

fxmlLoader.setLocation (getClass() getResource(templateFile));


and / i18n / messages

ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource ();
messageSource.addBasenames ("i18n/messages");

As I said, only when I run the app under IntelliJ!

 

IntelliJ IDEA 2018.2.5 (Ultimate Edition)
Build #IU-182.4892.20, built on October 16, 2018
---

---
JRE: 1.8.0_152-release-1248-b19 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

0
Comment actions Permalink

Steven,

Please report an issue on YouTrack attaching sample project for investigation: https://youtrack.jetbrains.com/issues/IDEA

 

0

Please sign in to leave a comment.