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版本与整个项目的版本不一致导致的。

相关推荐
风与沙的较量丶18 分钟前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_7482517218 分钟前
SpringBoot3 升级介绍
java
mcusun20001 小时前
VScode 使用Deepseek又方便又好用的另一款插件
ide·vscode·编辑器·deepseek
极客先躯2 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行2 小时前
Spring事务原理 二
java·后端·spring
小猫猫猫◍˃ᵕ˂◍2 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
liuyuzhongcc2 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
五月茶2 小时前
Spring MVC
java·spring·mvc
sjsjsbbsbsn2 小时前
Spring Boot定时任务原理
java·spring boot·后端
yqcoder2 小时前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript