I have a class in java that I intend to use from grails, what is the best way to do it?
I have a class in java that I intend to use from grails, what is the best way to do it?
Your class made in Java must comply with the following:
Here is an example of a class:
package edu.ltmj.prueba;
public class MiClase {
public String saluda(String nombre) {
return String.format("Hola %s", nombre);
}
}
Then, you must pack this class in a jar. To do this, you must compile the class and generate the respective jar. For example if you have the package and the class inside a folder src
, like this:
src
- edu
- ltmj
- prueba
+ MiClase.java
From the command line we can have the following:
$ cd /ruta/hacia/src
$ javac edu/ltmj/prueba/MiClase.java
$ jar -cvf miLib.jar edu/ltmj/prueba/MiClase.class
This will generate the jar with name miLib.jar
and that will contain the class MiClase
inside. You can import this jar into your groovy project and consume the classes and components that exist in your jar without problems. You can also generate the jar from your IDE (Eclipse, IntelliJ IDEA, NetBeans, etc).