How to run a Java class with a main method from Idea in project managed by Gradle in a modular way?
For class like
package org.example.moduledemo;
public class Main {
public static void main(String[] args) {
System.out.printf("Main module %s%n", System.getProperty("jdk.module.main"));
System.out.printf("Main module class %s%n", System.getProperty("jdk.module.main.class"));
System.out.printf("Module name %s%n", Main.class.getModule().getName());
}
}
when run in modular way using java --module-path '.\build\classes\java\main' --module org.example.moduledemo/org.example.moduledemo.Main, it prints
Main module org.example.moduledemo
Main module class org.example.moduledemo.Main
Module name org.example.moduledemo
However when a "Run 'Main.main()'" is selected from Idea editor context menu
it creates a Run configuration with class-path
Even when the "Use classpath of module" option is unchecked, and the configuration looks like
it still invokes the app using command-line
"C:\Program Files\Java\jdk-23\bin\java.exe" -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -cp C:\moduledemo-gradle\build\classes\java\main;C:\moduledemo-gradle\build\resources\main org.example.moduledemo.Main
and the application output shows that it's not run in modular way:
Main module null
Main module class null
Module name null
Invoking context menu "Run ...main()" action starts the application in modular way when the project is built by Idea itself (i.e. not managed by Gradle).
I'd like to keep using the Idea "Run ...main()" action for it's simplicity. I'm aware of workarounds like Gradle application plugin or Gradle exec task.




application { mainModule = ... }on the linked page, it doesn't seem to have any effect on behavior of Idea's Run action.runtask. Assumingjava.modularity.inferModulePathis true (the default in modern versions of Gradle) andapplication.mainModuleis set, then theruntask should handle setting the--module-pathand--moduleoptions for you.