Ran into a puzzling situation... My Groovy (4.0.18) app is working smoothly (thanks to all the help I received!) So with IntelliJ's help I packaged it up into a jar file including all dependencies and it works nicely - except when it tries to use java.util.Date -- at that point the behavior changes. If the app is executed through the jar file (java -jar myprogram.jar) then this exception is encountered -
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (String) values: [yyyy-MM-dd HH:mm:ss]
The code that it is trying to execute looks like -
static String timestamp() {
return new Date().format("yyyy-MM-dd HH:mm:ss")
}
However, when the same code is executed prior to packaging it into the jar file it works smoothly. All of the other Groovy functionality of the app works smoothly in either format; it accesses a database using Groovy Sql, it produces CSV files using Apache Commons CSV, parses the command line using CliBuilder, etc. Considerable Groovy functionality leveraging a good bit of Groovy enhancements all without issue - except for this one Date.format() situation.
My (limited) analysis so far is that Groovy is executing the code (based on the groovy.lang.MissingMethodException exception), and it is executing Java's java.util.Date class but is looking for a Java method named format() which takes a single String argument and Java does not have a format() method but the expectation is that Groovy provides an enhancement. According to the Groovy documentation -
String format(String format)
Create a String representation of this date according to the given format pattern.
String format(String format, TimeZone tz)
Create a String representation of this date according to the given format pattern and timezone.
Now in Java I think the situation is that instead of java.util.Date, java.util.DateFormat should be used - but this is Groovy and my understanding is that Groovy would take care of all that deprecation and Java details.
Simple Test Case Reproduction
I was able to reproduce the issue with an isolated Groovy script -
static String timestamp() {
return new Date().format("yyyy-MM-dd HH:mm:ss")
}
println "${timestamp()} - hello world"
The simple test case runs successfully in IntelliJ and from the command line outside of IntelliJ -
$ groovy datetimeformat.groovy
2024-02-29 14:02:23 - hello world
but when IntelliJ packages it up into a jar file then an execution of java -jar datetimeformat.jar results in -
$ java -jar datetimeformat.jar
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (String) values: [yyyy-MM-dd HH:mm:ss]
Looking at IntelliJ packaging, it looks like it packaged up all the Groovy components that the program uses (and maybe more, the datetimeformat.jar file has 14,485 files in it) but particularly including what looks like the Groovy Date enhancements -
Not sure how to debug this situation and would appreciate any help/guidance/recommendation.
