Search code examples
javaimportjarjmeterjwt

How do I import jwt.io Java library to JMeter


I am trying to import the jjwt Java library into JMeter but I get the following error;

ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: io for class: Script4

I am attempting to use the jjwt Java library https://github.com/jwtk/jjwt ... which can be found on the https://jwt.io/ website. I've added the files to the JMeter classpath and used a JSR223 sampler to write a script in Groovy... but it doesn't seem to accept it. I'm guessing that this is because it's expecting .jar files but the library is .java files, maybe?

Any theories on how to import this jjwt library would be greatly appreciated.

There is another post here; How to generate JWT token on JMETER using a RSA 256 private key Required library or jar file? that discusses this method and shows that it should work.


Solution

  • .java files are "text" files, they need to be compiled into .class files before you can run them in JVM. You can download pre-built .jar from Maven Central (make sure to fetch all the dependencies as well), or if you prefer the "hard" way:

    1. Install Apache Maven

    2. Create a file called pom.xml somewhere with the following content

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>org.example</groupId>
          <artifactId>jwt</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <properties>
              <maven.compiler.source>8</maven.compiler.source>
              <maven.compiler.target>8</maven.compiler.target>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>io.jsonwebtoken</groupId>
                  <artifactId>jjwt-api</artifactId>
                  <version>0.11.2</version>
              </dependency>
              <dependency>
                  <groupId>io.jsonwebtoken</groupId>
                  <artifactId>jjwt-impl</artifactId>
                  <version>0.11.2</version>
                  <scope>runtime</scope>
              </dependency>
              <dependency>
                  <groupId>io.jsonwebtoken</groupId>
                  <artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
                  <version>0.11.2</version>
                  <scope>runtime</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.bouncycastle</groupId>
                  <artifactId>bcprov-jdk15on</artifactId>
                  <version>1.60</version>
                  <scope>runtime</scope>
              </dependency>
          </dependencies>
      
      </project>
      
    3. Open that folder in the terminal application

    4. Execute mvn dependency:copy-dependencies command

    5. Copy everything from target/dependency folder to "lib" folder of your JMeter installation (or other location in JMeter Classpath)

    6. Restart JMeter

    7. Add JSR223 Sampler to your Test Plan and put your Groovy code using the jjwt library functions there.