Search code examples
javagrails

Error when calling Java code in Grails


I have issues with calling the java class from grails application.

Controller class:

class MyController{

    def index() { 
         somepkg.MyJavaClass.method()
    }
}

Java class:

package somepkg;

public class MyJavaClass{

    public void method() {
           // ... some logic here
    }
}

The error:

No signature of method: static somepkg.MyJavaClass.method() is applicable for argument types: () values: [] Possible solutions: wait(), any(), find(), wait(long), each(groovy.lang.Closure), find(groovy.lang.Closure)


Solution

  • If you want to call a method of a class without creating an instance, the method has to be marked static:

    public static void afada() throws FileNotFoundException, UnsupportedEncodingException{
      PrintWriter writer = new PrintWriter("ediOrder.txt", "UTF-8");
      writer.println("a");
      writer.close();
    }
    

    or you have to create an instance first:

    new examplepkg.testing().afada()