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

相关推荐
小旋风-java6 分钟前
springboot整合dwr
java·spring boot·后端·dwr
JAVA坚守者12 分钟前
Maven常见解决方案
java·maven
聊天宝快捷回复23 分钟前
必收藏,售后客服日常回复必备的话术 (精华版)
java·前端·数据库·经验分享·微信·职场发展·快捷回复
wanyuanshi25 分钟前
map的键排序方法
java·数据结构·算法
热爱前端的小wen28 分钟前
maven的介绍与安装
java·spring·maven·springboot
追风小老头折腾程序1 小时前
Java单体服务和集群分布式SpringCloud微服务的理解
java·后端·spring·spring cloud
java_heartLake1 小时前
设计模式之观察者模式
java·观察者模式·设计模式
码农幻想梦1 小时前
Visual Studio Code下载安装及汉化
ide·vscode·编辑器
Bruce_Liuxiaowei1 小时前
VSCode远程切换Python虚拟环境
ide·vscode·python