Langage Groovy

Voir aussi :

https://developers.redhat.com/articles/2023/07/06/how-use-debezium-smt-groovy-filter-routing-events

Hello world

#! /usr/bin/env groovy /* This is an example */ println "Hello World !"

Importer une class

/* #from java.util import Date #from java.text import SimpleDateFormat */ import java.util.Date import java.text.SimpleDateFormat

Fichier java .class

test.groovy

#! /usr/bin/env groovy println "Test1" class echo1 { def name; String hello() { println "Test2" return name } }
groovyc test.groovy

$ strings test.class | grep -i test
test
test.groovy
Ltest;
Test1
$ file *.class
echo1.class: compiled Java class data, version 49.0 (Java 1.5)
test.class: compiled Java class data, version 49.0 (Java 1.5)

$ groovy --classpath=. test
Test1
$ groovy --classpath=. test.groovy
Test1

Pb

Pb WARNING: An illegal reflective access operation has occurred

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/usr/share/groovy/lib/groovy-2.4.21.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Pb Error: Could not find or load main class test

$ java -cp . test
Error: Could not find or load main class test
Caused by: java.lang.NoClassDefFoundError: groovy/lang/Script

Solution

java -cp /usr/share/groovy/lib/groovy-2.4.21.jar:$PWD test

Pb Error: Main method not found in class xxx

$ java -cp /usr/share/groovy/lib/groovy-2.4.21.jar:$PWD echo1 Error: Main method not found in class echo1, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Solution

Voir : https://www.yawintutor.com/main-method-not-found-javafx-application-class/

test.groovy

#! /usr/bin/env groovy package com.plop; public class Hello { public static void main(String[] args) { System.out.println("Hello World"); } }
java -cp /usr/share/groovy/lib/groovy-2.4.21.jar:$PWD com/plop/Hello