2

Below is my complete code that I entered in Jmeter BeanShell Sampler.

BufferedReader fileReader = newBufferedReader(new FileReader("F:/url.txt"));
int counter = 1;
content = fileReader.readLine();
while ((nextLine = fileReader.readLine()) != null)
{
content = content + "\n" + nextLine;
counter++;
}
vars.put("content",content);

I am getting below error when I try to execute, I can see this error in log.info

2019-12-02 11:38:01,431 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``BufferedReader fileReader = newBufferedReader(new FileReader("F:/url.txt")); int . . . '' : Typed variable declaration : Command not found: newBufferedReader( java.io.FileReader )
2019-12-02 11:38:01,432 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``BufferedReader fileReader = newBufferedReader(new FileReader("F:/url.txt")); int . . . '' : Typed variable declaration : Command not found: newBufferedReader( java.io.FileReader )

I just copy pasted this code from online examples and similarly I tried so many different option to read a file, but it's not working, could someone please help me.

I am using windows machine and jmeter version 5.1

Rafi Shiek
  • 53
  • 1
  • 5

2 Answers2

1

You have a typo in your code, you need to change this:

newBufferedReader

to this:

new newBufferedReader

as the instantiation assumes using Java new operator


P.S. Since JMeter 3.1 users are recommended to user JSR223 Test Elements and Groovy language for scripting, the reasons are in:

  • Groovy has better performance than Beanshell
  • Groovy is more modern language which supports all new Java features while with Beanshell you're stuck at Java 1.5 language level
  • Groovy provides a lot of enhancements over normal Java SDK

assuming all above you can convert your code to this:

vars.put("content", new File("F:/urls.txt").text)

P.P.S. It is also recommended to avoid scripting where possible and use JMeter's built-in components for maximum performance so if you only need to read a file into a JMeter Variable you can consider using __FileToString() function like:

${__FileToString(F:/urls.txt,,content)}

Check out Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

I changed the code, and below is the updated code, it works for me, I changed the first line of code, given space between new and BufferedReader.

BufferedReader fileReader = new BufferedReader(new FileReader("F:/url.txt"));
int counter = 1;
content = fileReader.readLine();
while ((nextLine = fileReader.readLine()) != null)
{
content = content + "\n" + nextLine;
counter++;
}
vars.put("content",content);
log.info(content);
Rafi Shiek
  • 53
  • 1
  • 5