I tried Groovy TimeCategory Mixin according to Groovy Coodbook
import groovy.time.TimeCategory
Integer.metaClass.mixin TimeCategory
Date.metaClass.mixin TimeCategory
footballPractice = 1.week.from.now - 4.days + 2.hours - 3.seconds
println footballPractice
It works fine in the groovy console, but I get a StackOverError, when I run it as a script
groovy MyScript.groovy
Using
Groovy Version: 2.1.8 (via GVM tool, but the same problem with system default 1.8.x)
JVM: 1.7.0_40 Vendor: Oracle Corporation
OS: Linux, Ubuntu 13.04, 64bit
I will double check but I don't think mixin is recommended anymore. Try
use(TimeCategory) {
footballPractice = 1.week.from.now - 4.days + 2.hours - 3.seconds
println footballPractice
}
Related
In groovy I was using below to generate date in the format "2020-09-14T06:00:00Z" in Ready API 3.3.1
def today = new Date()
return today.format("yyyy-MM-dd") + "T" + today.format("HH:mm:ss") +".000Z"
After updating to 3.3.2 It's giving me an error
groovy.lang.MissingMethodException: No signature of method: java.util.Date.format()
is applicable for argument types: (String) values: [yyyy-MM-dd].
Could someome help
In recent versions of Groovy the Date extension methods have been moved to their own library, which you will have to add to your project. For example, if you are using Gradle as your build tool, you may want something like this:
compile group: 'org.codehaus.groovy', name: 'groovy-dateutil', version: '3.0.5'
If using Maven:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-dateutil</artifactId>
<version>3.0.5</version>
</dependency>
I try to add HttpBuilder into groovy script, but can do it only manually (Alt+Ctrl+Shift+S add dependencie). But when I start script I have error in line of creating new httpbuilder instance java.lang.ClassNotFoundException: org.apache.http.client.HttpClient. I manualy add HttpClient, butClassNotFoundException: net.sf.json.JSONObject and so on. But when I add Ini library it works fine.
I also tried to use #Grab
main()
def main() {
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
And have compilation error
Error:Groovyc: While compiling GroovyTests: java.lang.RuntimeException: Error grabbing Grapes -- [download failed: net.sf.json-lib#json-lib;2.3!json-lib.jar]
And net in def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org') is red and Cannot resolve a symbol 'net' error
will be glad to any help
Since you have now installed the groovy executables as per the comments, the following code:
#Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('https://jsonplaceholder.typicode.com')
def res = http.get(path: '/users')
println "Number of users: ${res.size()}"
should now run and print:
─➤ groovy solution.groovy
Number of users: 10
─➤
(tested on Groovy Version: 2.5.8 JVM: 1.8.0_232 Vendor: AdoptOpenJDK OS: Linux)
One thing that might be disrupting the artifact resolution is if you have a custom grapeConfig.xml file. This file (if it exists) should be under <user home dir>/.groovy/grapeConfig.xml and the default text that groovy uses if no grapeConfig.xml file is present can be found here:
https://github.com/apache/groovy/blob/master/src/resources/groovy/grape/defaultGrapeConfig.xml
In addition, if you need to debug grapes downloads, you should try the following flags (again as mentioned in the comments):
─➤ groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 yourGroovyScript.groovy
which should print information on what grapes are actually doing when the resolution fails.
What does your groovy -v look like? i.e. what version of groovy and what jdk are you on?
I just started using Checker Framework and have a problem that is exactly reproducible on one of the example projects from authors of this framework. This project is available here:
https://github.com/typetools/checker-framework/tree/master/docs/examples/GradleExamples
When i run this command from root:
>gradle compileJava
i receive this compilation output:
public static /*#Nullable*/ Object nullable = null;
^
required: #Initialized #NonNull Object
list.add(null); // error on this line
^
required: #Initialized #NonNull String
2 errors
:compileJava FAILED
As you can see there is no any information about where errors occur like class name, line number in code etc.
I did not find any information in their official manual about any compiler parameters that can change output format appropriately. I want error messages look like this:
~\GradleExample.java:33 error: ';' expected
UPDATE:
I achieve this behaviour on 3 machines:
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_73;
Gradle: 2.14.
OS: Microsoft Windows 10 x64 Pro [version 10.0.14393];
Java: 1.8.0_121;
Gradle: 3.4.1.
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_121;
Gradle: 3.4.1.
The absence of line numbers and class names is experienced only when running with Gradle. I also tried to run checker with Maven and with Javac from command line and it worked perfectly.
To configure Checker Framework with Gradle i followed steps from manual. There are 3 steps:
Download framework;
Unzip it to create a checker-framework directory;
Configure Gradle to include Checker Framework on the classpath.
As i understand, Gradle will do steps 1 and 2 automatically when providing needed Checker Framework's jars through dependency management. Nevertheless i tried both options:
dependency management:
I simply downloaded example project and executed "gradle compileJava" from root
of the GradleJava7Example project.
manually writing paths in gradle build file:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar",
"-Xbootclasspath/p:C:\\checker-framework-2.1.10\\checker\\dist\\jdk8.jar",
'-classpath', 'C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar;C:\\checker-framework-2.1.10\\checker\\dist\\javac.jar'
]
}
}
I've found a workaround. I'll explain it later, but now if somebody has the same problem, add this line to you JavaCompile tasks configuration:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
System.setProperty("line.separator", "\n") // <<<<<< add this line
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "${configurations.checkerFramework.asPath}",
"-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
]
}
}
First of all i must say that problem was not in Checker Framework at all. I managed to reproduce the same behavior as mentioned in question without Checker Framework. I have created a little custom Annotation Processor. Here is the code:
#SupportedSourceVersion(value = SourceVersion.RELEASE_8)
#SupportedAnnotationTypes(value = {"*"})
public class MyProcessor extends AbstractProcessor{
#Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
String sepr = System.getProperty("line.separator");
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "[error code] " + sepr + " catched!!!");
return true;
}
}
As you can see, all it does is printing a message right away from start. Note that i used a line separator provided by java.lang.System class to split message. When i registered this processor and tried to run "gradle compileJava" from gradle project it produced the following output:
:compileJava
catched!!!
1 error
:compileJava FAILED
The property "line.separator" for Windows OS returns CR+LF: "\r\n". I don't know why Messager.printMessage(Diagnostic.Kind kind, CharSequence msg) has this behaviour, because when i type System.err.print("[error code] " + sepr + " catched!!!") instead, everything works fine (note also that this problem occur only when i use Gradle, if i run manually javac with all arguments or use Maven everyting is fine).
I found that if i substitude the provided by system separator with simple "\n" symbol compiler error messages are displayed correctly.
For now i choose this solution as a workaround.
Have the following errors for the following scripts in a Mule flow when I run the application in CloudHub. When I run it on Anypoint studio, there's no issues.
I checked the deployed application archive file and in the classes folder is the compiled company.cloudtools.util.Utilities class. I am not sure why the CloudHub server cannot locate the class file as defined by the error.
Error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script16.groovy: 1: unable to resolve class company.cloudtools.util.Utilities
# line 1, column 1.
import company.cloudtools.util.Utilities
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:858)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:548)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:497)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:214)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngin
Script in Mule flow:
<scripting:transformer doc:name="parse record type">
<scripting:script engine="Groovy"><![CDATA[import company.cloudtools.util.Utilities
import com.netsuite.webservices.platform.core.types.RecordType
import org.mule.module.netsuite.RecordTypeEnum
// flowVars[ns_record_type] is used for the "get-record" operation
flowVars["ns_record_type"] = Utilities.findRecordType(payload["ns_record_type"], Arrays.asList(RecordType.values()), 0)
// flowVars[ns_record_type_enum] is used for the "update record" operation?
flowVars["ns_record_type_enum"] = Utilities.findRecordTypeEnum(payload["ns_record_type"], Arrays.asList(RecordTypeEnum.values()), 0)
return payload]]></scripting:script>
</scripting:transformer>
Utilities Class:
package company.cloudtools.util;
import com.netsuite.webservices.platform.core.types.RecordType;
import org.mule.module.netsuite.RecordTypeEnum;
import java.util.*;
public class Utilities {
//initial call... findRecordType("downloaditem", RecordType.values(), 0)
public static RecordType findRecordType(String bad_name, List<RecordType> available_enums, Integer current_character) {}
//initial call... findRecordTypeEnum("downloaditem", RecordTypeEnum.values(), 0)
public static RecordTypeEnum findRecordTypeEnum(String bad_name, List<RecordTypeEnum> available_enums, Integer current_character) {}
}
The issue is that CloudHub only supports JRE 1.6 for the version of Mule I am using, Mule 3.5.0. Mule workers that are 3.5.1 above uses the currently supported JRE 1.7. Had the change my applications JRE to get it to work.
As a later task, I will find out how to make it work with JRE 1.7 (probably have to update the runtime environment to 3.5.1 or above).
See the Technical / Troubleshooting section at Mulesoft - http://www.mulesoft.org/documentation/display/current/FAQ
So far here are the things I've checked from the server:
DB2 JDBC Driver - check
DB2 License (db2jcc_license_cu.jar) included in class path - check
DB2 db2java.zip included in class path - check
DB2 db2jcc.jar included in class path - check
Here's the current class path:
CLASSPATH=/home/db2cae/sqllib/java/db2java.zip:/home/db2cae/sqllib/java/sqlj.zip:/home/db2cae/sqllib/function:/home/db2cae/sqllib/java/db2jcc_license_cu.jar:/home/db2cae/sqllib/tools/clpplus.jar:/home/db2cae/sqllib/tools/antlr-3.2.jar:/home/db2cae/sqllib/tools/jline-0.9.93.jar:/home/db2cae/sqllib/java/db2jcc.jar:.
JAVA_BINDIR=/usr/lib64/jvm/jre/bin
JAVA_HOME=/usr/lib64/jvm/jre
JAVA_ROOT=/usr/lib64/jvm/jre
The command that was executed was:
java -cp ./db2jcc.jar com.ibm.db2.jcc.DB2Jcc -version
However it returned
What could be the possible problem for this?
i assume your classpath environment is not considered.
You could try someting like:
Windows:
java -cp %CLASSPATH%;./db2jcc4.jar com.ibm.db2.jcc.DB2Jcc -version
Linux:
java -cp $CLASSPATH:./db2jcc4.jar com.ibm.db2.jcc.DB2Jcc -version