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

相关推荐
Zane199428 分钟前
遍历时删元素为什么报错:modCount 这个"计数器"在背后盯着你
java·后端
电商API_180079052471 小时前
速卖通商品采集API技术文章
java·开发语言·c++·api·跨境电商·商品详情
郑州光合科技余经理1 小时前
海外版外卖系统架构:订单怎么流转、权限怎么分
java·开发语言·前端·系统架构·uni-app·php·ai编程
一木 之林2 小时前
五、C++新特性、关键字与编译原理
java·jvm·算法
han_hanker2 小时前
新手公司项目实战
java·maven
vHelios2 小时前
【电商项目】商品搜索开发复盘(3):搜索与封装实现逻辑思考与编写
java
AI人工智能+电脑小能手2 小时前
大白话说Java设计模式-37-中介者模式(源码剖析篇)
java·设计模式·线程池·中介者模式·源码分析·executor·applicationeventmulticaster
焦虑的说说3 小时前
虚拟线程浅析
java
摇滚侠3 小时前
《SpringBoot 3:入门与应用实战》第 7 章 AOP 思想与实现 阅读笔记 13
java·spring boot·笔记