Code working in Test Case, but not in Keyword

Aloha All. I am struggling with why my code works in the test case itself, but when I created a keyword it does not.

Test Case:
import java.nio.file.StandardCopyOption as StandardCopyOption
import java.io.IOException as IOException
import java.nio.file.Files as Files
import java.nio.file.Path as Path
import java.nio.file.Paths as Paths
Path sourceDir = Paths.get(GlobalVariable.dept152srcfolder)
assert Files.exists(sourceDir)
Path sourceFile = Paths.get(GlobalVariable.dept152srcfolder + GlobalVariable.dept152srcfile_1)
assert Files.exists(sourceFile)
Path targetDir = Paths.get(GlobalVariable.dept152importfolder)
assert Files.exists(targetDir)
Path targetFile = Paths.get(GlobalVariable.dept152importfolder + GlobalVariable.dept152srcfile_1)
Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING) //copy with REPLACE_EXISTING option
assert Files.exists(targetFile)

The above runs just fine. The file shows up in the targetDir.

I want to create a keyword of the above and then pass the parameters like so:
CustomKeywords.‘global.filefolderops.copyFileToFolder’(GlobalVariable.dept152srcfolder, GlobalVariable.dept152srcfile_1, GlobalVariable.dept152importfolder)

Keyword is:
import java.nio.file.StandardCopyOption
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
public class filefolderops {

@Keyword
def copyFileToFolder(String sourceDir, String sourceFile, String targetDir) {
	println('Source Directory: ' + sourceDir)
	println('     Source File: ' + sourceFile)
	println('Target Directory: ' + targetDir)
	Path targetFile = Paths.get(GlobalVariable.dept152importfolder + GlobalVariable.dept152srcfile_1)
	println('     Target File: ' + targetFile)
	//assert Files.exists(sourceDir)
	//assert Files.exists(sourceFile)
	//assert Files.exists(targetDir)
	//Path path = Files.copy(sourceFile, targetFile,StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option
	Files.copy(sourceFile, targetFile,StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option
	//assert Files.exists(targetFile)
}

}

I get:
2022-11-03 11:03:41.293 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: groovy.lang.MissingMethodException: No signature of method: static java.nio.file.Files.copy() is applicable for argument types: (java.lang.String, sun.nio.fs.WindowsPath, java.nio.file.StandardCopyOption) values: [152-CA_DUE1_5Records.txt, \wc1na01\WalzEnvFS\INTSE\CertifiedPro\ImportFolders\152\152-CA_DUE1_5Records.txt, …]
Possible solutions: copy(java.nio.file.Path, java.nio.file.Path, [Ljava.nio.file.CopyOption;), copy(java.nio.file.Path, java.io.OutputStream), copy(java.io.InputStream, java.nio.file.Path, [Ljava.nio.file.CopyOption;), any(), notify(), wait()
2022-11-03 11:03:41.302 ERROR c.k.katalon.core.main.TestCaseExecutor

I have spent several hours googling and trying to figure this out, but am not understanding what the issue was. I saw some posts saying the signature error meant I was not passing the same number of parameters that the keyword was expecting, but I am passing 3 and expect 3. I saw mentions that maybe the keywords cannot accept more than one parameters, but that seems unreasonable.

Thank you in advance for any help anyone can provide. This is driving me nuts!
Dave

The error MissingMethodException means that you do not have the correct parameters of the method–either not enough parameters or the wrong data type for the parameters.

Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option

Here are the expected data types for the method (taken right from the error message):

Possible solutions:

  1. copy(java.nio.file.Path, java.nio.file.Path, [Ljava.nio.file.CopyOption;), // the one you’re using
  2. copy(java.nio.file.Path, java.io.OutputStream),
  3. copy(java.io.InputStream, java.nio.file.Path, [Ljava.nio.file.CopyOption;)

Since you are passing in String for the sourceFile instead of Path in the definition of your method, you need to change the String data type to Path.

def copyFileToFolder(String sourceDir, String sourceFile, String targetDir) {

should be:

def copyFileToFolder(String sourceDir, Path sourceFile, String targetDir) {

@Dave_Weil1

Please use the “Code Formatting syntax” — enclose your code with a pair of triple backticks — for the better readability of codes.

@Dave_Weil1

    @Keyword
    def copyFileToFolder(String sourceDir, String sourceFile, String targetDir) {
        ...
        Files.copy(sourceFile, targetFile,StandardCopyOption.REPLACE_EXISTING);
        ...

You are passing a "String sourceFile" to a statement "Files.copy(sourceFile". But "Files.copy(Path" requires a java.nio.file.Path object as the 1st argument. Therefore your code fails. You must give a variable of type java.nio.file.Path to the 1st argument of Files.copy() method.


You are paying attention only to the number of parameters. You are not paying any attention to the type of parameters — java.lang.String or java.nio.file.Path. Too primitive mistake. It seems you need more of basic trainings for Java/Groovy language.

You should stop Katalon Studio now. You should buy some good Java programming book (like Core Java, Vol1) and read it from the Chapter 1 to understand what “the type of parameters” means.