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

相关推荐
Java小Y9 分钟前
redis(2)-java客户端使用(IDEA基于springboot)
java·redis·intellij-idea
herderl9 分钟前
【无标题】命名管道(Named Pipe)是一种在操作系统中用于**进程间通信(IPC)** 的机制
java·linux·服务器·嵌入式硬件·php
笑衬人心。30 分钟前
缓存的三大问题分析与解决
java·spring·缓存
用户84913717547161 小时前
JDK 17 实战系列(第7期):迁移指南与最佳实践
java·jvm
duration~1 小时前
SpringAI实现Reread(Advisor)
java·人工智能·spring boot·spring
我们从未走散1 小时前
面试题-----微服务业务
java·开发语言·微服务·架构
YuforiaCode1 小时前
24SpringCloud黑马商城微服务整合Seata重启服务报错的解决办法
java·spring·微服务
考虑考虑2 小时前
JDK21中的Sequenced Collections(序列集合)
java·后端·java ee
一 乐2 小时前
心理咨询|学生心理咨询评估系统|基于Springboot的学生心理咨询评估系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·学生心理咨询评估系统