Vam Vam - 11 days ago 8
Groovy Question

Jenkins Groovy script works in Windows but not Linux

The below code is working fine in Windows (groovy script shell of jenkins) but when i execute it on groovy script shell of jenkins in Linux its throwing the error:


groovy.lang.MissingMethodException: No signature of method:
sun.nio.fs.UnixPath.withReader() is applicable for argument types:
(Script1$_run_closure1) values: [Script1$_run_closure1@71c12adb] at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at
org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:8) at
org.jenkinsci.plugins.scriptler.util.GroovyScript.call(GroovyScript.java:122)
at hudson.remoting.LocalChannel.call(LocalChannel.java:45) at
org.jenkinsci.plugins.scriptler.util.ScriptHelper.runScript(ScriptHelper.java:124)
at
org.jenkinsci.plugins.scriptler.util.ScriptHelper.runScript(ScriptHelper.java:89)
at
org.jenkinsci.plugins.scriptler.ScriptlerManagement.doTriggerScript(ScriptlerManagement.java:470)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)


Here's the code; I pass the value for
Select_Folders
:

import groovy.json.JsonSlurper
import java.nio.file.Paths

JsonSlurper slurper = new JsonSlurper()
def folder_struct

Paths.get('C:\\Json_Results\\results.json').withReader { reader ->
folder_struct = slurper.parse(reader)
}

return folder_struct [Select_Folders]


Could anyone please help me with the problem? Of course, I have changed the path
C:\\Json_Results\\results.json
to
/home/project/results.json
when running on Linux.

Answer

So Path extensions were only added to Groovy in v2.3.0

I suspect you have an old version of Groovy on your Jenkins nodes.

You should just be able to use a File instead:

def folder_struct = new File('C:\\Json_Results\\results.json').withReader { reader ->
    slurper.parse(reader)
}
Comments