Reputation: 1093
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;
}
}
Upvotes: 3
Views: 3550
Reputation: 1093
As dmahapatro pointed out, it's a compilation order issue. So I read Mixed Java and Groovy Applications and altered my program slightly. I created Pet.java
:
public interface Pet {
public String getName();
public String getNickname();
}
Then I renamed my Groovy class Pet
to PetImpl
and had it extend the new Pet
interface. Now the Java code can compile independently of Groovy, so it does and then the Groovy code compiles and everything works.
EDIT: I've played with this some more, and I discovered that if I right-click my main
method in the Groovy file and create a Run As configuration (Groovy Script) which directly references main class com.example.Test
, I no longer experience this problem, without having to make any changes to the code I posted originally (i.e., having no Java Pet
interface).
Upvotes: 1
Reputation: 50275
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.
Upvotes: 2