文章目录






一、错误记录



运行

class Student1 {
}

Groovy 代码 , 会报错 ;



报错信息 :

groovy.lang.GroovyRuntimeException: This script or class could not be run.
It should either:
- have a main method,
- be a JUnit test or extend GroovyTestCase,
- implement the Runnable interface,
- or be compatible with a registered script runner. Known runners:
* <none>

【错误记录】IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )_问题分析






二、问题分析



只有符合如下要求的 Groovy 代码可以运行 :

  • 有 main 函数 ;
    【错误记录】IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )_Groovy_02

  • 测试用例 , 作为JUnit测试或扩展GroovyTestCase ;
    【错误记录】IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )_intellij-idea_03

  • 线程相关 , 实现Runnable接口 ;
    【错误记录】IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )_Groovy_04

  • 与注册的脚本运行程序兼容 ;
    【错误记录】IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )_Gradle_05

其它情况的 Groovy 代码 , 均不可运行 ;