IntelliJ not loading resources with Gradle project on Java 14
Answered
Hi All,
I am porting an app built with Gradle to Java 14 where:
- code is under `src/main/java`,
- I have a `module-info.java` file under `src/main/java`,
- resources are under `src/main/resources`
I am trying to load a resource from the classpath as follows:
URL url = getClass().getResource(path);
if (url == null) {
url = getClass().getClassLoader().getResource(path);
}
When running the app from within IntelliJ, I am getting the following error:
Exception in thread "main" java.lang.NullPointerException
at neoho/neoho.NeoHoFrame.initComponents(NeoHoFrame.java:36)
at neoho/neoho.NeoHoFrame.<init>(NeoHoFrame.java:17)
at neoho/neoho.App.main(App.java:8)
So the resource is not found on the path. I have tried various paths:
- module/resource.png
- /module/resource.png
- resource.png
- /resource.png
and no joy. How should I configure my Gradle build or IntelliJ to be able to load the resources correctly? Note that this runs fine on Netbeans for example.
Repro project can be found here: https://github.com/tychobrailleur/neoho
Here is what the IntellJ config (2020.1.1 CE) looks like:
More details on the env:
- Gradle:
./gradlew -version
------------------------------------------------------------
Gradle 6.4.1
------------------------------------------------------------
Build time: 2020-05-15 19:43:40 UTC
Revision: 1a04183c502614b5c80e33d603074e0b4a2777c5
Kotlin: 1.3.71
Groovy: 2.5.10
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 14.0.1 (Oracle Corporation 14.0.1+7)
OS: Mac OS X 10.14.6 x86_64
- Java:
java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7)
OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
- IntelliJ
IntelliJ IDEA 2020.1.1 (Community Edition)
Build #IC-201.7223.91, built on April 30, 2020
Runtime version: 11.0.6+8-b765.40 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6
GC: ParNew, ConcurrentMarkSweep
Memory: 1981M
Cores: 8
Non-Bundled Plugins: Org4Idea, org.jetbrains.erlang, org.jetbrains.idea.clojure, org.intellij.scala, org.jetbrains.plugins.rest, org.asciidoctor.intellij.asciidoc
Please sign in to leave a comment.
Hello,
Looks like https://youtrack.jetbrains.com/issue/IDEA-197469
Thanks, this helped. For posterity, I have applied the fix that worked for me there as per the ticket:
https://github.com/tychobrailleur/neoho/commit/1f99a9ca3c0d26df3b387ce249f4e1e55bb306f6
So just to confirm, this seems to be an IntelliJ issue rather than a Gradle one in the handling of resources, following from those two threads:
?
Yes, it's an issue on IDE side. We're working on it.
Has this issue been fixed?
No, https://youtrack.jetbrains.com/issue/IDEA-197469 is still open.