Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've been using Apache JMeter quite a lot more lately and have been finding it a very useful tool for automating some repetitive tasks that we have to perform around system maintenance on one of my projects. However, not everything can be done with what's available in JMeter out of the box and sometimes you just need to run some external Java code from a JAR file.

For various reasons I'm still using JMeter 2.x, but this same approach should work in later versions.

In the JMeter install directory there is a lib directory and an ext directory within that. I like to place any external JAR into the lib/ext directory. JMeter does need to be restarted after copying the JAR file over, it will not detect changes automatically.
jmeter_jar2.png


In this case I'm using the CaseUtils class from the Apache Commons Text API as an example. The JAR file for it can be downloaded from here.
jmeter_jar.png


Using a JSR223 Sampler, and the language set to Java, the API can be imported and used as required. Here's just some sample code to demonstrate that it works...
 Java
import org.apache.commons.text.*;
char[] delims = {' '};
String myString = CaseUtils.toCamelCase("my example", true, delims);
log.info("====> " + myString);


When running this JMeter project, the log viewer will display output generated. It is important to note that all code must be at the Java 1.4 language level, which means you can't use lambdas, try-with-resources, for-each, etc. Just plain old Java code.



-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.