JMH ERROR: Unable to find the resource: /META-INF/BenchmarkList

The JMH error “Unable to find the resource: /META-INF/BenchmarkList” is mostly observed when we do not configure the compiler plugin to use the JMH annotation processor during the sourcecode compilation.

1. Problem

If we do not include the JMH annotation processor during compilation and then we run the class with JMH annotations (such as @Benchmark), we will get the following error in runtime:

Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
	at org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:98)
	at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:124)
	at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:253)
	at org.openjdk.jmh.runner.Runner.run(Runner.java:209)
	at org.openjdk.jmh.Main.main(Main.java:71)
	at com.howtodoinjava.core.basic.ForLoopPerformanceTest.main(ForLoopPerformanceTest.java:15)

2. Solution

To fix the issue, include the JMH annotation processor in the compiler plugin and rebuild the project.

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
      <source>17</source>
      <target>17</target>
      <annotationProcessorPaths>
        <path>
          <groupId>org.openjdk.jmh</groupId>
          <artifactId>jmh-generator-annprocess</artifactId>
          <version>1.35</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
<plugins>

In Maven, to rebuild the project, run the clean install command:

$ mvn clean install

After we do both steps and run the JMH benchmark class, it will run just fine.

3. JMH Configuration Example

The following are the main configuration and dependency elements for JMH in a normal Java project. Please verify that you have configured all the pieces correctly.

<properties>
    <jmh.version>1.35</jmh.version>
</properties>

<dependencies>
	<dependency>
	    <groupId>org.openjdk.jmh</groupId>
	    <artifactId>jmh-core</artifactId>
	    <version>${jmh.version}</version>
	</dependency>
	<dependency>
	    <groupId>org.openjdk.jmh</groupId>
	    <artifactId>jmh-generator-annprocess</artifactId>
	    <version>${jmh.version}</version>
	</dependency>
</dependencies>

<build>
	<plugins>

	    <plugin>    
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-compiler-plugin</artifactId>
	        <version>3.8.1</version>
	        <configuration>
	            <source>17</source>
	            <target>17</target>
	            <annotationProcessorPaths>
	                <path>
	                    <groupId>org.openjdk.jmh</groupId>
	                    <artifactId>jmh-generator-annprocess</artifactId>
	                    <version>${jmh.version}</version>
	                </path>
	            </annotationProcessorPaths>
	        </configuration>
	    </plugin>

	    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <finalName>benchmarks</finalName>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.openjdk.jmh.Main</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

	</plugins>
</build>

Please share whether this solution worked for you or not in the comments.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial