10

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

UBIK LOAD PACK
  • 31,356
  • 4
  • 58
  • 100
Karim Narsindani
  • 364
  • 3
  • 11
  • 32

1 Answers1

2

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.

user7294900
  • 47,183
  • 17
  • 74
  • 157
  • 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. – Karim Narsindani Sep 19 '17 at 12:01
  • JMeter did that for you internally – user7294900 Sep 19 '17 at 12:40
  • You mean to say that, vars is ready made object of class which is available to access in jmeter? I want to implement similar feature for my GUI application. What would you suggest? – Karim Narsindani Sep 19 '17 at 13:16
  • As I understand it you are using JMeter as an example here - if you have a class in your Java application which exposes get() and put() methods, like a map, then you can do "bshInterpreter.set("myMap", javaMyMapObject);" then in Beanshell do "myMap.get("aField");" – Dazed Sep 22 '17 at 13:39
  • The code (from JMeter) @user7294900 has provided follows this pattern for "vars". – Dazed Sep 22 '17 at 13:41
  • I have created class which has get, set method but when I import it in beanshell script it does not work. My all class file are available in bin folder. For sample and easy to understand see my code in question. When, I create object of interpreter in class, get set works file, but as jmeter beanshell sampler, beanshell script will be in other file which will be called on runtime. Why, my class file is not getting imported in beanshell. I have tried all the possible way to import class. Any idea – Karim Narsindani Sep 23 '17 at 17:41
  • Class should be inside jar in test package . The jar located in **lib** folder and not bin folder – user7294900 Sep 24 '17 at 04:29
  • Also To create class you should call `c= new BSClass();` instead of `c=BSClass();` – user7294900 Sep 24 '17 at 05:17