Search code examples
jmeterbeanshell

Jmeter BeanShellSampler error: Error invoking bsh method: eval import org.apache.commons.io.FileUtils


I use BeanShell code loading 100s of sql files in jmeter:

   import org.apache.commons.io.FileUtils;
    
    File folder = new File("D:\\sql99");
    File[] sqlFiles = folder.listFiles();
    for (int i = 0; i < sqlFiles.length; i++) {
        File sqlFile = sqlFiles[i];
        if (sqlFile.isFile()) {
            vars.put("query_" + i,sqlFile.getName(), 
             FileUtils.readFileToString(sqlFiles[i]));
        }
    }

but get error info :

17:42:03,301 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.commons.io.FileUtils; File folder = new File("D:\sql99"); Fi . . . '' : Error in method invocation: Method put( java.lang.String, java.lang.String, java.lang.String ) not found in class'org.apache.jmeter.threads.JMeterVariables'

I want to get each sql execute time in jmeter results tree. How to fix code?

Thanks!


Solution

  • You're trying to call JMeterVariables.put() function which accepts 2 Strings as the parameters passing 3 Strings

    The correct syntax is vars.put("variable-name", "variable-value"); so you need to decide how to amend this line:

    vars.put("query_" + i, sqlFile.getName(), FileUtils.readFileToString(sqlFiles[i]));
    

    so it would contain only 2 parameters instead of 3.

    Also since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting mainly for performance reasons so it might be a good option for switching (the same code will work in Groovy without changes assuming you fix the issue with vars.put() function call)