August 28, 201411 yr Hello ( excuse my english, I'm French )I use scriptmaster file to develop functions.However, I observe that sometimes a function works fine in the file and not when I record with RegisterGroovy, and the reverse is also true.for example this function works properly when it is saved with RegisterGroovy, not in the file.how to develop functions properly in this uncertain environment ? what is this error ? the code example : import java.util.zip.ZipOutputStream import java.util.zip.ZipEntry import java.nio.channels.FileChannel String zipFileName = Zip_path String inputDir = Dir_path Try { ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(zipFileName)) new File(inputDir).eachFile() { file -> zipFile.putNextEntry(new ZipEntry(file.getName())) def buffer = new byte[1024] file.withInputStream { i -> def l = i.read(buffer) // check whether the file is empty if (l > 0) { zipFile.write(buffer, 0, l) } } zipFile.closeEntry() } zipFile.close() } catch(Exception e){ // if any error occurs e.printStackTrace() } and the error of scriptmaster file org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 24: expecting EOF, found 'catch' @ line 24, column 7. } catch(Exception e) { ^
August 28, 201411 yr …just a matter of typo, your script will compile flawlessly if you write the symbol 'Try' of the try-catch statement in lowercase.
August 28, 201411 yr Author Yes it is > thanks Clem Now I try to inject a wrong dir path and scriptmaster file does't return any error why ?you develop codes preferentially with an IDE?I just tried the groovy console and it looks to be working well
August 28, 201411 yr Author removing the "e.printStackTrace ()" and replacing it with "return 'erreur'" it works and if I put 'error' scriptmaster opens an empty window but it does not prevent to create an empty zipI will see controlling the directory before with an if
August 28, 201411 yr Author well this code works fine without writing the zip if there's error //ZipFolder ( Dir_path ; Zip_path ) import java.util.zip.ZipOutputStream import java.util.zip.ZipEntry import java.nio.channels.FileChannel try { File f = new File ( Dir_path ) if ( f.isDirectory() ) { ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(Zip_path)) new File(Dir_path).eachFile() { file -> zipFile.putNextEntry(new ZipEntry(file.getName())) def buffer = new byte[1024] file.withInputStream { i -> def l = i.read(buffer) // check whether the file is empty if (l > 0) { zipFile.write(buffer, 0, l) } } zipFile.closeEntry() } zipFile.close() } else { return "erreur" } } catch(Exception e) { // if any error occurs return "erreur" //e.printStackTrace() }
August 28, 201411 yr well this code works fine without writing the zip if there's error … but will fail if your target directory contains subdirectories.
Create an account or sign in to comment