博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
groovy main method is use static main(args) //ok
阅读量:6003 次
发布时间:2019-06-20

本文共 2730 字,大约阅读时间需要 9 分钟。

Groovy classes and scripts

Groovy has done a lot to make Java more palatable, but at the same time has introduced some confusion as to what developers can get away with at the most fundamental level.
Living on The Main Line
In Java, main methods are typically, if not always
public static void main(String[] args)

In Groovy, methods are public by default and args is typed dynamically, so you can get away with

static main(args)  //ok

but not

static main() //not okvoid main() //not okvoid main(args) //not ok

attempting to run a class with these bad main method declarations, the compiler will invariably return

Caught: groovy.lang.GroovyRuntimeException: This script or class could not be run.It should either:- have a main method,- be a JUnit test, TestNG test or extend GroovyTestCase,- or implement the Runnable interface.

IntelliJ is clever in that it can recognize the proper declaration on the fly. If you missing the required main method declaration you will get the normal Groovy class file type icon

If you have a valid main method you will see the runnable arrow
Groovy scripts
I have some sturdy bags I take with me to the grocery store. If I buy lot of food they just give me a paper bag in which to put the extra items. Every item is treated the same regardless of which bag they put it in.
A Groovy script is essentially any file with some code outside the confines of a class. Scripts can contain their own classes but all the stuff outside those is given its own class based on the filename and placed inside a working main(args) method. They just give you another bag - it's no big deal.
The following Groovy class and Groovy script are equivalent.
Groovy class with main method

class aGroovyClass {  def myString = "I am an instance variable";  static main(args) {    def myObject = new aGroovyClass();//def required    println myObject.myString;}}

Groovy script with class

class aGroovyClassWithinAScript {  def myString = "I am an instance variable";}myObject = new aGroovyClass(); //no def neededprintln myObject.myString;

Multiple main methods - you had to ask
Can classes inside a script have their own main method? Umm, yes, but that is pretty confusing (even to IntelliJ). I would have to explicitly call that main method from my class to use it - main is not a constructor.
Class with its own main method.
IntelliJ again
Both Groovy scripts and Groovy classes with main methods should be run as Groovy Script in the Run/Debug Configuration. Sometimes IntelliJ will default to attempting to run it as a Java application. If you attempt to run a Groovy script as a Java application you will get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: main

转载地址:http://nfsmx.baihongyu.com/

你可能感兴趣的文章
烂泥:LVM学习之逻辑卷LV及卷组扩容VG
查看>>
9. ZooKeeper之搭建单机模式。
查看>>
紧急维护,阿里云服务器抢修记
查看>>
数字货币相关
查看>>
payload和formData有什么不同?
查看>>
131016
查看>>
第六次作业
查看>>
python 自动化测试HTTP接口
查看>>
常用排序算法
查看>>
题解——loj6280 数列分块入门4 (分块)
查看>>
Nginx配置文件nginx.conf详解
查看>>
Ubuntu下实现socks代理转http代理
查看>>
使用PL/SQL能查询oracle中数据,在for update 语句中一直卡住
查看>>
05机器学习实战之Logistic 回归scikit-learn实现
查看>>
libevent evbuffer参考文章
查看>>
用python爬取app照片
查看>>
ASP.NET状态管理
查看>>
三生万物:决策树
查看>>
Javascript计算器(Calculator) 利用Javascript计算形如“(8*(2*(2+3)*2)*10)*10 ”表达式的值...
查看>>
java学习(二)对象与类
查看>>