I am going to
explain How to call java method from Groovy and call Groovy method from java Briefly
1.
Create the following class with name HelloJava
like this and save file name as HelloJava.java
public class HelloJava{public void sayHello() {
System.out.println("Hello I am a method from java!");
}
}
2. Create the following class with name HelloGroovylike this and save file name as HelloGroovy.groovy
public class HelloGroovy{
public void sayHello() {
println("Hello I am a method from groovy!");
}
}
3. Create the following class with name MainGroovy this and save file name as MainGroovy.groovy
public class MainGroovy {
public static void main(String []args) {
HelloJava helloJava = new HelloJava()
helloJava.sayHello();
}
}
4. Create the following class with name MainJava this ans save file name as MainJava.java
public class MainJava {
public static void main(String []args) {
HelloGroovy helloGroovy = new HelloGroovy();
helloGroovy.sayHello();
}
}
5. compile java class HelloJava.java with the following command "javac HelloJava.java"
6. compile groovy class HelloGroovy.grovvy with the following command "groovyc HelloGroovy.groovy"
7. compile java class MainGroovy.groovy with the following command "javac MainGroovy.groovy "
8. compile java class MainJava.java with the following command "javac MainJava .java"
9. run java class MainJava with the following command "java -cp the_path_of_your_groovy_library_located; MaiJava" . the method in groovy will be called and the output will be shown as below:
10. run groovy class MainGroovy with the following command "groovy MainGroovy" .
The method in java will be called and the output will be shown as below:
I hope this will be helpful !
No comments:
Post a Comment