How to run Java code in in JMeter and pass vairables from java code to Jmeter script

1.1k views Asked by At

I am reading xlsx file by adding Beanshell Preprocessor. When i run code in Eclispe, it's working fine.

But when i run in Jmeter, i am getting below error. I have copied required jar files in Jmeter lib and lib\ext as well.

2015/06/08 14:53:04 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval

In file: inline evaluation of: import java.io.File; import java.io.FileInputStream; import java.io.IOException; . . . '' Encountered "=" at line 18, column 41. 2015/06/08 14:53:04 WARN - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``import java.io.File; import
java.io.FileInputStream; import java.io.IOException; . . . '' Encountered "=" at line 18, column 41."

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class url {

    public static void main(String[] args) throws IOException {

        FileInputStream file=new FileInputStream(new File("C:\\temp\\project.xlsx"));

        XSSFWorkbook workbook=new XSSFWorkbook(file);
        XSSFSheet sheet=workbook.getSheetAt(0);
        Iterator<Row>rowIterator=sheet.iterator();

        int count=1;
        while(rowIterator.hasNext()){
            Row row=rowIterator.next();
            Iterator<Cell> cellIterator=row.cellIterator();
            while(cellIterator.hasNext()){
                Cell cell=cellIterator.next();
                String TextInCell=cell.toString();
                String cellContent1="Cricket";
                String cellContent2="Football";
                String cellContent3="F1";
                String cellContent4="Badminton";
                String cellContent5="Misslenous";

                if(TextInCell.contains(cellContent1)){
                    String var=cell.getRichStringCellValue().toString();
                    String Category = cellContent1;

                }else if(TextInCell.contains(cellContent2)){
                    String var=cell.getRichStringCellValue().toString();
                    String Category = cellContent2;

                }else if(TextInCell.contains(cellContent3)){
                    String var=cell.getRichStringCellValue().toString();
                    String Category = cellContent3;

                }else if(TextInCell.contains(cellContent4)){

                    String var=cell.getRichStringCellValue().toString();
                    String Category = cellContent4;
                    System.out.println(var + "----"+Category );

                }else{
                    String var=cell.getRichStringCellValue().toString();
                    String Category = cellContent5;

                }

            }

        }

    }


}
1

There are 1 answers

1
Dmitri T On
  1. Beanshell is not very Java, you need to amend your code to match Beanshell conventions to wit:

    • remove "class" and "main" method
    • remove <Cell> from Iterator<Cell>
    • remove <Row> from Iterator<Row>
    • explicitly cast Objects like Row row=(Row)rowIterator.next();
  2. Beanshell has well-known performance problems, if you need to deal with Excel files I would recommend going for JSR223 PreProcessor and "groovy" language instead. To enable "groovy" support just download groovy-all.jar and drop it to the /lib folder of your JMeter installation
  3. JMeter restart is required to pick up groovy or POI or whatever jars.
  4. See How to Extract Data From Files With JMeter guide for more information on dealing with binary files, hopefully you'll get some clues.