Static Main method in groovy script file

Source: Internet
Author: User
Tags command line groovy script

This article describes the static Main method in groovy. First look at the following code:

class Test1 {
    public Test1() {
         println  "TEST1"
     }
}

class Test2 {
    public Test2()  {
         println "TEST2"
     }
    static void main(args)  {
        new Test1()
     }
}

This code does not run with either the Groovy command line or the Groovyconsole, as if it were a reference

Groovy.lang.MissingMethodException:No signature of Method:Test1.main () is applicable for argument types: ([ljava.lang.s Tring;) values: {[]}

At Test1.invokemethod (SCRIPT0)

And take a look at the new code:

class Test2 {//含有static void main的方法的Test2必须要在第一个定义
     public Test2() {
         println "TEST2"
     }
    static  void main(args) {
        new Test1()
     }
}
class Test1  {
    public Test1() {
         println "TEST1"
     }
}

The meaning of this is needless to say:

In groovy scripts, there should be no classes that define multiple static main methods. If you have more than one class defined, you should put the class with the main method in the first place;D

These are the static main methods in the groovy script file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.