简体   繁体   English

将 Java 代码传递给 Beanshell 时出现编译错误

[英]Compilation error when passing Java code to Beanshell

So, I am using BeanShell PostProcessor to write the json data to csv file.所以,我使用 BeanShell PostProcessor 将 json 数据写入 csv 文件。 This is my code in beanshell postprocessor.这是我在 beanshell 后处理器中的代码。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.opencsv.CSVWriter;

public class CsvWrite{
    public static void main(String[] args){
String response = prev.getResponseDataAsString();
        JSONParser parser = new JSONParser();
        File file = new File("D:\\verisk\\first_url_response.csv");

        try {
            FileWriter fileWriter = new FileWriter(file);
            CSVWriter csvWriter = new CSVWriter(fileWriter);

            // adding header to csv
            String[] header = { "id", "title", "description", "price", "discountPercentage", "rating", "stock",
                    "brand" };
            csvWriter.writeNext(header);

            Object obj = parser.parse(response);
            JSONObject jsonObject = (JSONObject) obj;

            JSONArray productArray = (JSONArray) jsonObject.get("products");

            int productArraySize = productArray.size();
            for (int i = 0; i < productArraySize; i++) {
                JSONObject productDetailObject = (JSONObject) productArray.get(i);
                String id = (productDetailObject.get("id").toString());
                String title = (productDetailObject.get("title").toString());
                String price = (productDetailObject.get("price").toString());
                String description = (productDetailObject.get("description").toString());
                String discountPercentage = (productDetailObject.get("discountPercentage").toString());
                String rating = (productDetailObject.get("rating").toString());
                String stock = (productDetailObject.get("stock").toString());
                String brand = (productDetailObject.get("brand").toString());

                String[] productDetails = { id, title, description, price, discountPercentage, rating, stock, brand };
                csvWriter.writeNext(productDetails)
            }
            fileWriter.close();

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

However, it does not write to csv. on using the log command following error is shown.但是,它不会写入 csv。在使用日志命令时会显示以下错误。

2023-01-02 14:41:06,853 ERROR oajuBeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``import java.io.File; 2023-01-02 14:41:06,853 错误 oajuBeanShellInterpreter:调用 bsh 方法时出错:eval 在文件中:内联评估:``导入 java.io.File; import java.io.FileNotFoundException;导入 java.io.FileNotFoundException; import java.io.FileWr.导入 java.io.FileWr。 . . . . '' Encountered "}" at line 44, column 25. '' 在第 44 行第 25 列遇到“}”。

2023-01-02 14:41:06,853 INFO oajtJMeterThread: Thread is done: Thread Group 1-1 2023-01-02 14:41:06,853 INFO oajtJMeterThread: Thread finished: Thread Group 1-1 2023-01-02 14:41:06,854 INFO oajeStandardJMeterEngine: Notifying test listeners of end of test 2023-01-02 14:41:06,854 INFO oajguJMeterMenuBar: setRunning(false, local ) 2023-01-02 14:41:06,853 信息 oajtJMeterThread:线程完成:线程组 1-1 2023-01-02 14:41:06,853 信息 oajtJMeterThread:线程完成:线程组 1-1 2023-01-02 14: 41:06,854 信息 oajeStandardJMeterEngine:通知测试听众测试结束 2023-01-02 14:41:06,854 信息 oajguJMeterMenuBar:setRunning(假,本地

Note: I have added json-simple-1.1.jar and opencsv-4.1.jar in lib folder.注意:我在 lib 文件夹中添加了 json-simple-1.1.jar 和 opencsv-4.1.jar。 The code is working with java in eclipse.该代码与 eclipse 中的 java 一起使用。

This line:这一行:

Encountered "}" at line 44, column 25.在第 44 行第 25 列遇到“}”。

means that you have extra } which doesn't match the opening one so I don't think the code is the "same"意味着你有额外的}与开头的不匹配所以我不认为代码是“相同的”

Also you will need to call the main function explicitly, Beanshell doesn't have an entry point and processes the code upside down.此外,您还需要显式调用 main function,Beanshell 没有入口点并颠倒处理代码。

Another thing is thatsince JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting另一件事是,由于 JMeter 3.1 建议使用 JSR223 测试元素和 Groovy 语言编写脚本

and finally if you run your code with 2 or more threads you will face a race condition resulting in data corruption and/or loss so I would rather suggest extracting the values from JSON using JSON Extractor and writing them into the CSV file using Flexible File Writer最后,如果您使用 2 个或更多线程运行代码,您将面临导致数据损坏和/或丢失的竞争条件,因此我宁愿建议使用JSON 提取器从 JSON 中提取值,并使用Flexible File Writer将它们写入 CSV 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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