XillercoreX
XillercoreX

Reputation: 21

How to execute java class in jar file from Jmeter

I am fresher in Jmeter I have created a two class as

*package test;
public class Urlmap {
    static String turl=null;
    public String display(){
        String url="/xyz";
        Test2 t=new Test2(url);
        turl=t.x;
        return "/xyz";

    }
}
package test;
public class Test2 {
static String x=null;
Test2(String x){
    this.x=x;
}   
}*

I have imported the jar then trying to execute the class in BeanShell Sampler of Jmeter

import test.Urlmap;
Urlmap u =new Urlmap();
log.info("xxxxxxxxxxxx :----"+u.display());
log.info("turl :----"+u.turl);

it is giving me error as --Error invoking bsh method: eval Sourced file: inline evaluation of: import test.Urlmap; Urlmap u =new Urlmap(); log.info("xxxxxxxxxxxx :----"+u.di . . . '' : Cannot access field: turl, on object: test.Urlmap@16ec122a 2017/07/28 06:44:56 WARN - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:import test.Urlmap; Urlmap u =new Urlmap(); log.info("xxxxxxxxxxxx :----"+u.di . . . '' : Cannot access field: turl, on object: test.Urlmap@16ec122a

But it working fine in Eclipse. Is Jmeter can access one class value at a time not nested class value .

Upvotes: 2

Views: 7288

Answers (3)

Dmitri T
Dmitri T

Reputation: 168002

Remember, Beanshell != Java. Moreover, it is not the best scripting option as Beanshell interpreter has known performance problems.

So I would strongly recommend switching to JSR223 Sampler and Groovy language as Groovy's Java compatibility is much higher and Groovy engine has much better performance due to being able to compile well-behaved scripts into bytecode and caching the compiled scripts to speed up consecutive executions. See Apache Groovy - Why and How You Should Use It for more details.

With Groovy you will be able to use your code "as is"

JMeter Groovy Example

However accessing static fields via instance reference isn't a Java good practice, so I would recommend amending your code to

import test.Urlmap;
Urlmap u =new Urlmap();
log.info("xxxxxxxxxxxx :----"+u.display());
log.info("turl :----"+Urlmap.turl); 

Upvotes: 4

Ori Marko
Ori Marko

Reputation: 58774

If you want turl to remain static, just add public so it can be access by Jmeter

 public static String turl = null;

Also static field should be called with class name , use:

 Urlmap.turl

Upvotes: 0

Usagi Miyamoto
Usagi Miyamoto

Reputation: 6289

The problem is, that your turl field has a scope of package protected: It is only visible in the package test, but not from JMeter's package.

Solution: Replace static with public.

Upvotes: 0

Related Questions