0
votes
import java.io.*;
import java.text.SimpleDateFormat;  
import java.util.ArrayList;
import java.util.*;


public class createFXRates_Files {
    public static void main(String[] args)throws IOException 
    {
        int totalrecords = Integer.parseInt(args[0]);
        int randomrow;
        String FileName = null;


        //Creating a file 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        FileName = "FXrates."+sdf.format(date);
        File outfile = new File("C:\\SAMIR\\Projects\\DataHub_BMO\\JAVATEST\\"+FileName);
        if(outfile.exists()) {
            outfile.delete();
        }
        outfile.createNewFile();

        FileWriter fw = new FileWriter(outfile);

        //Reading a sampler file
        Scanner sc = new Scanner(new File("C:\\SAMIR\\Projects\\DataHub_BMO\\FXrates.20190903"));// sample file path to scanner
        ArrayList<String> rows= new ArrayList<String>();
        while(sc.hasNext()){
            String data = sc.nextLine();
            rows.add(data);
        }

        Random random = new Random();

        for(int i=0; i<totalrecords; i++) {

            randomrow = random.nextInt(10000);
            System.out.println(""+Integer.toString(randomrow));

            fw.write(""+rows.get(randomrow)+"\n");
            System.out.println(rows.get(randomrow));
        }

        sc.close();
        fw.close();

    }
}

I need to convert above java code in a bean shell sampler in jmeter. tried doing it resulted into "In file: inline evaluation of: ``import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayLis . . . '' Encountered "=" at line 27, column 39. " error. it seems like compiler cannot resolve for Collection classes. even though code include an import of "import java.util.ArrayList;"

2

2 Answers

1
votes

Beanshell doesn't support diamond operators in particular and stuck at Java SE 1.5 language level in general therefore you need to remove these <String> bits in order to make the code working.

Be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting, in this case you will not have to change anything, moreover Groovy has much better performance comparing with Beanshell and adds some "syntax sugar" on top of Java SDK

0
votes

It is better to use JSR223 Sampler instead of Beanshell Sampler. In JSR223 Sampler: Put the following code in script area

    import java.io.*;
    import java.text.SimpleDateFormat;  
    import java.util.ArrayList;
    import java.util.*;

    int totalrecords = Integer.parseInt(args[0]);
    int randomrow;
    String FileName = null;


    //Creating a file 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date date = new Date();
    FileName = "FXrates."+sdf.format(date);
    File outfile = new File("C:\\SAMIR\\Projects\\DataHub_BMO\\JAVATEST\\"+FileName);
    if(outfile.exists()) {
        outfile.delete();
    }
    outfile.createNewFile();

    FileWriter fw = new FileWriter(outfile);

    //Reading a sampler file
    Scanner sc = new Scanner(new File("C:\\SAMIR\\Projects\\DataHub_BMO\\FXrates.20190903"));// sample file path to scanner
    ArrayList<String> rows= new ArrayList<String>();
    while(sc.hasNext()){
        String data = sc.nextLine();
        rows.add(data);
    }

    Random random = new Random();

    for(int i=0; i<totalrecords; i++) {

        randomrow = random.nextInt(10000);
        log.info(""+Integer.toString(randomrow));

        fw.write(""+rows.get(randomrow)+"\n");
      log.info(rows.get(randomrow));
    }

    sc.close();
    fw.close();