Search code examples
jmetercommand-line-interface

JMeter - JSONObject not found in namespace error through CLI only


I am experiencing an error using a Java/Beanshell 2.0b6 when running a script through the CLI on a Macbook, which does not occur when running through the GUI;

2023-12-04 16:52:47,858 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor - Create tokenPayload
javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager;   import org.apach . . . '' : Typed variable declaration : Class: JSONObject not found in namespace : at Line: 10 : in file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager;   import org.apach . . . '' : JSONObject 
 in inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager;   import org.apach . . . '' at line number 10
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) ~[ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:45) [ApacheJMeter_components.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.2]
    at java.lang.Thread.run(Thread.java:1589) [?:?]

The org.json .jar file exists in /lib and the JMETER_HOME classpath seems to be correctly set to the installation directory. Could anyone please advise what may be the issue and why the jar cannot be found at runtime?

(Note - Appreciate I should be using a JSR223 / Groovy but there is a bit of a disconnect between the Java version (19) and JMeter version (5.4.2) stopping me from doing this for the code in question)

Would just like to understand why there is a disconnect between the GUI and the CLI


Solution

    1. There should not be "disconnect", most probably you're running different JMeter versions for "CLI" and "GUI" modes, compare jmeter.log files for both and you should see the full path to JMeter binaries, Classpath, etc.
    2. If you want to use Groovy you could update Groovy libraries in "lib" folder of your JMeter installation or downgrade to Java 17.
    3. It's recommended to always use the latest version of JMeter so consider upgrading to JMeter 5.6.2 or whatever is the latest stable version which is available at JMeter Downloads page.