Java with Beanshell to access fields and object wi

2019-06-15 19:15发布

1). I know how to access the java fields and object in beanshell from my question Use java class fields in beanshell. However, it is not so clean way to implement as I need to first set the java variable in beanshell and then I can use it. However, in Jmeter it provides very clean way of using maps in beanshell similar way as we do in java, but JMeter has developed it's know library (class) which helps to access get/put methods for maps. I want to achieve similar way to access Map in beanshell.

I have checked JMeter for more information and I want to know that, I have created user define variable temp and assign value error, now in BSF process I just write a line vars.put('Name','temp Value') and it has updated value for temp variable. So, the question is I have not created JMeterVariables object vars but still beanshell allows to update values in map without setting any values as mention in your answer. I want to know how this works, need more depth information.

2). I have created my own class in java and in beanshell I am importing this class but it is giving Command not found: BSClass() below is the entire code

Java class

package test;

public class BSClass {

public void BSCMethod(){
    System.out.println("I am from BSClass method BSCMethod");
    }
}

sample.bsh

import test.BSClass;

c=BSClass();
c.BSCMethod();
print("I am from BeanShell Script");

Calling sample.bsh file java class

package test;

import java.io.FileNotFoundException;
import java.io.IOException;
import bsh.*;

public class DynamicVariable {
   public static void main(String[] args) throws FileNotFoundException, IOException, EvalError {
    new bsh.Interpreter().source("\\src\\test\\sample.bsh");
   }
}

Note:

  1. I don't need help in JMeter, it is to use in core java and beanshell.
  2. All the files are in my project.
  3. BSClass.class is under bin folder of my project

I would appreciate your inputs

1条回答
神经病院院长
2楼-- · 2019-06-15 19:52

In Beanshell you can add any Object you want including a Map

In JMeter, JMeterVariables is special implementation of Map that is added to Beanshell Interpreter before evaluate and also special Object as JMeterContext is added which even includes JMeterVariables inside. Code:

    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();

    try {
        bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
        bshInterpreter.set("Label", getName()); //$NON-NLS-1$
        bshInterpreter.set("prev", jmctx.getPreviousResult());//$NON-NLS-1$
        bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
        bshInterpreter.set("vars", vars);//$NON-NLS-1$

In your case with map you can do similar as you describe in comment:

 bshInterpreter.set("myMap", javaMyMapObject);"

Then in Beanshell get the specific key from map:

 myMap.get("aField");

To create class you should use new keyword, call:

c= new BSClass();

instead of c=BSClass();

If you create your own class, Class should be inside jar in relevant package .

The jar should be located in lib folder and not in bin folder, see JMeter's getting started:

Any jar file in such a directory will be automatically included in user.classpath, jar files in sub directories are ignored. The given value is in addition to any jars found in the lib directory. All entries will be added to the class path of the system class loader and also to the path of the JMeter internal loader.

查看更多
登录 后发表回答