Idea开发Gradle多模块项目踩坑纪实之一:Java模块无法执行

最近尝试了使用Idea尝试使用Gradle开发多模块项目,结果遇到一个见鬼的问题:

首先,总项目是一个空项目。

第二,我首先建立了两个Java模块,然后建立了一个Spring boot程序模块。

当我建立第四个Java模块的时候发现问题了,系统直接生成的Java程序居然不能运行,报告:

复制代码
Execution failed for task ':cn.edu.cup.Main.main()'.
> Process 'command 'C:\Users\xpli1\.jdks\graalvm-jdk-17.0.12\bin\java.exe'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 310ms
2 actionable tasks: 2 executed

这样的错误。

系统生成的样例代码如下:

java 复制代码
package cn.edu.cup;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
        // to see how IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");

        for (int i = 1; i <= 5; i++) {
            //TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
            // for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
            System.out.println("i = " + i);
        }
    }
}

然后,关闭整个项目,单独打开这一模块的时候,运行正常。

通过对比几个模块gradle.build文件的差异发现,其他模块中都有指定Java版本的 句子,添加了这些语句后,成功运行。

具体的语句为:

java 复制代码
java {
    // 指定源码和目标代码版本
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

分析原因,估计是新建的项目缺省的java版本与整个项目的版本不一致导致的。

相关推荐
饺子大魔王的男人19 小时前
Remote JVM Debug+cpolar 让 Java 远程调试超丝滑
java·开发语言·jvm
VernonJsn1 天前
visual studio 2022的windows驱动开发
ide·驱动开发·visual studio
阿尔帕兹1 天前
Visual Studio2019/2022离线安装完整教程(含闪退解决方法)
ide·visual studio
萌止1 天前
Visual Studio下载保姆级安装教程
ide·visual studio
GISer_Jing1 天前
解决Visual Studio调试时找不到exe文件的终极指南
ide·visual studio
程序人生5181 天前
Visual Studio 2026下载与安装详细教程!(附下载地址)
ide·visualstudio·visual studio
Hx_Ma161 天前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet
期待のcode1 天前
原子操作类LongAdder
java·开发语言
舟舟亢亢1 天前
Java集合笔记总结
java·笔记
小酒窝.1 天前
【多线程】多线程打印ABC
java