1

I noticed that I am not able to get a resource when I run/debug a modularized Java 11 application within IntelliJ, but when I use Gradle's run task it works fine.

LoadResource.java

package com.example;

public class LoadResource {
    public static void main(String ... args) {
        new LoadResource().run();
    }

    private void run() {
        System.out.println("TEST");

        final var resource = getClass().getResource("Foo.txt");
        System.out.println(resource.toExternalForm());
    }
}

module-info.java

module LoadResource {
    exports com.example;
}

build.gradle

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

mainClassName = 'LoadResource/com.example.LoadResource'

UPDATE: Adding

run {
    doFirst {
        println commandLine
    }
}

shows that Gradle apparently executes [C:\Program...\bin\java.exe, -Dfile.encoding=windows-1252, -Duser.country=US, -Duser.language=en, -Duser.variant, -cp, D:\xxx\load-resource\build\classes\java\main;D:\xxx\load-resource\build\resources\main, null].

You can find my sample project here.

Hannes
  • 5,002
  • 8
  • 31
  • 60
  • @nullpointer First of all: I do not expect anything from people. Secondly, last time I had a question about an IntelliJ-related problem I was asked to provide a sample project (and that is what I would expect in return if I was to answer such a question). Lastly, I believe I explained my problem in detail and provided the file just as an additional information. But thanks for your time anyways :-) – Hannes Dec 06 '18 at 14:05
  • @nullpointer I am not sure that any of those articles answer my question. The first problem sounds quite similar but then they are talking about how to get it working in Gradle, but that is not my problem. My problem is that it does not work if I run it within IDEA, but I might be completely wrong. – Hannes Dec 06 '18 at 14:55
  • The only possible difference there could be how `gradle` is executing your module Vs how `IntelliJ` does. In that case, could you update the question with the effective command java command for both cases. I am quite sure it would be a named vs unnamed module execution. Would reopen the question if none of that matches the explanation from an existing link. – Naman Dec 06 '18 at 15:00
  • @nullpointer I added what I believe is the actual Gradle command line command. – Hannes Dec 06 '18 at 15:30
  • If I could read that correct. `-cp, D:\xxx\load-resource\build\classes\java\main` possibly means that your module is loaded as an unnamed module. – Naman Dec 06 '18 at 15:58
  • So my actual problem would be: How to load resources from an named module? – Hannes Dec 06 '18 at 16:08
  • @Hannes The first link posted as existing answers to your question indeed gives you exactly the solution to your problem. Try to run the gradle task with `--info`, and you will see the options that are used. Hint: See what the plugin [does](https://github.com/java9-modularity/gradle-modules-plugin/blob/master/src/main/java/org/javamodularity/moduleplugin/tasks/RunTask.java#L85) with the resources folder. You will have to add the same to the VM options of your run/debug configuration. – José Pereda Dec 06 '18 at 20:15
  • @JoséPereda And that is the strange thing, I already tried this and added as VM args in IntelliJ `--patch-module LoadResource=D:\WINDOWS\IdeaProject\load-resource\out\production\resources --module LoadResource/com.example.LoadResource`, but with that, I get `java.lang.module.FindException: Module LoadResource not found` and therefore I am wondering if I do anything completely wrong here. (plus: I would expect an expensive IDE doing that stuff for me, automatically.) – Hannes Dec 06 '18 at 20:45
  • I've just added this: `--patch-module LoadResource=build/resources/main`, and that works for me. Don't need to add the module, it will be added. There is an order in the arguments that has to be followed. – José Pereda Dec 06 '18 at 20:49
  • 1
    @JoséPereda Oh ... THAT worked for me, too! btw: I am writing on an article about my pain with migrating from JDK 8 to JDK 11 so maybe someone else can profit from all the information I gained on the journey :-) Once again, thank you for your so much appreciated help! – Hannes Dec 06 '18 at 20:53
  • @Hannes, could you link your article? – elect Nov 26 '19 at 12:33

0 Answers0