Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy code cannot find Java constructor

When I run the main method in the Groovy code below, I get a groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.example.Person(java.lang.String, com.example.Pet). Why is that? I'm using the latest version of the Groovy/Grails Tool Suite.

Groovy:

package com.example

import groovy.transform.TypeChecked

@TypeChecked
class Test {
    static main(args) {
        Pet fido = new Pet(name: 'Fido', nickname: 'Scruffy')
        Person dave = new Person('Dave', fido)
    }
}

@TypeChecked
class Pet {
    String name
    String nickname
}

Java:

package com.example;

public class Person {
    private String name;
    private Pet pet;

    public Person(String name, Pet pet) {
        this.name = name;
        this.pet = pet;
    }
}
like image 204
Kiwi Avatar asked Apr 22 '26 05:04

Kiwi


1 Answers

Make sure you compile and run in this order:

  • groovyc Pet.groovy
  • javac Person.java
  • groovyc Test.groovy

  • groovy Test

It works for me as expected if the above order is followed. In GGTS make sure you are compiling/building the project as expected to make sure dependent classes are built.

like image 98
dmahapatro Avatar answered Apr 24 '26 18:04

dmahapatro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!