简体   繁体   中英

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 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 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 .

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示例

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); 

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM