27

println() inside static void main method is not printing anything anywhere, whereas only println() prints in terminal. Here is my code:

class CalcMain {
static void main(def args) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");

    println("testing");
  }
}

And when I ran it shows pass (Green Triangle in Jmeter) but doesnt print anything on terminal

Whereas a simple program such as

println("testing");

prints on terminal.

Could someone please let me know where I am doing wrong?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Pratik
  • 952
  • 2
  • 16
  • 31

5 Answers5

33

Don't use System.out.println in a Groovy or Beanshell step in jmeter. Instead , do this:

1. Enable the stdout console in Jmeter so that you can see the output.
2. Use  log.info("Message:" + vars.get("variableName"));  instead.
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 2
    In jMeter, there is possible to write to both: To the print("stdout") as well as to the log.info("log"). Your answer shows the logging, but the original question asks for stdout. How to write to the stdout, separately from the log, independently? – Franta Oct 19 '17 at 11:46
  • how to enable the stdout console in jmeter? – user269867 Jan 19 '21 at 05:15
  • There is a little button in the Jmeter UI in the upper right corner that opens the log console stdout – djangofan Jan 19 '21 at 21:04
11

Use below to display text in JMeter Response Tab in "Results Tree":

SampleResult.setResponseData("Document: " + variable,"UTF-8");

Use below to log in the console text area of JMeter:

log.info("hello");

A 3rd option is to use Java standard:

System.out.println("Any String");

Above can be written from any Java/Groovy/JSR223 sampler. But it will be printed on the Parent CMD window that opened the JMeter UI in windows.

sumon c
  • 739
  • 2
  • 10
  • 18
3

Chapter "2. OUT" shows the answer:

https://jmetervn.com/2016/12/05/jsr223-with-groovy-variables-part-1/

use the OUT command.

OUT.println("INPUT MESSAGE HERE");
Franta
  • 986
  • 10
  • 17
  • This answer and the link provided helped me print values of variables from JSR223 samplers to the console directly (read Jenkins active project build, ofc non-gui mode) – Mahesh Nov 04 '18 at 11:55
0

Try it: System.out.println("testing")

Jay
  • 1,389
  • 1
  • 10
  • 15
0

You have not provided all code, just your part produces errors as imports are also needed. Definition of class does not run by its own, you need as instance and then run in explicitly, then it prints (both with and w/out System.out. prefix) to terminal:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

class CalcMain {
static void main(def args) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");

    println ("testing1");
    System.out.println ("testing2");
  }
}

CalcMain test1 = new CalcMain();
test1.main();
println ("testing3");

Output:

testing1
testing2
testing3
Alex Martian
  • 3,423
  • 7
  • 36
  • 71