Error: Could not find or load main class org.openjdk.jmh.runner.ForkedMain
Asked Answered
P

2

9

I am benchmarking some methods using jmh through a spring boot application. When i run it through IDE it gives me benchmarks but when i package it with maven and run it through CMD it gives the below stack trace.

# Run progress: 0.00% complete, ETA 00:11:00
# Fork: 1 of 1
Error: Could not find or load main class org.openjdk.jmh.runner.ForkedMain
<forked VM failed with exit code 1>
<stdout last='20 lines'>
</stdout>
<stderr last='20 lines'>
Error: Could not find or load main class org.openjdk.jmh.runner.ForkedMain
</stderr>

Benchmark had encountered error, and fail on error was requested
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.openjdk.jmh.runner.RunnerException: Benchmark caught the exception
        at org.openjdk.jmh.runner.Runner.runBenchmarks(Runner.java:578)
        at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:320)
        at org.openjdk.jmh.runner.Runner.run(Runner.java:209)
        at com.test.statistics.metrics.DruidBenchmarkApplication.main(DruidBenchmarkApplication.java:56)
        ... 8 more
Caused by: org.openjdk.jmh.runner.BenchmarkException: Benchmark error
        at org.openjdk.jmh.runner.Runner.doFork(Runner.java:771)
        at org.openjdk.jmh.runner.Runner.runSeparate(Runner.java:663)
        at org.openjdk.jmh.runner.Runner.runBenchmarks(Runner.java:561)
        ... 11 more
        Suppressed: java.lang.IllegalStateException: Forked VM failed with exit code 1
                ... 14 more




**POM.XML:**

<?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>com.test.stats</groupId>
    <artifactId>druid-metrics</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>druid-metrics</name>
    <description>Demo project for benchmarking druid</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <docker.image.prefix>druid-metrics</docker.image.prefix>
        <jmh.version>1.21</jmh.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20080701</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.4</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

I have tried setting classpath explicitly through info from bunch of forums but that didn't help running the application through cmd. Any help would be appreciated.

Thanks!

Perpetua answered 21/8, 2018 at 15:30 Comment(3)
Spring boot works by shading your artifact into it's wrapper (launcher). JMH works by shading your artifact into it's wrapper (launcher). You might have a conflict in artifact processing. Check your configuration.Alkylation
jmh-generator-annprocess is provided? by who?Alkylation
I appear to be having the same issue with the jar file created that bundles all the jmh classes. Unzipping the classes to a local directory and specifying it as a path does not load the class either.Pugliese
S
2

Had same ClassNotFoundException with Gradle + JMH which was an incompatibility bug fixed in recent versions (jmh-gradle-plugin >= 0.5.0). As blase as this advice is: try upgrading (or downgrading) and seeing if it still occurs.

Strader answered 9/11, 2019 at 21:28 Comment(0)
W
0

Use assembly plugin instead of spring boot plugin to package a fat jar. For me, it works:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>${mainClass}</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The root cause is that the root directory of Spring Boot's fat jar is set to BOOT-INF

Watershed answered 12/8 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.