melix / jmh-gradle-plugin

Integrates the JMH benchmarking framework with Gradle
Apache License 2.0
666 stars 88 forks source link

jmhJar - "Could not find or load main class org.openjdk.jmh.Main" #133

Closed leventov closed 5 years ago

leventov commented 6 years ago

When I use jmhJar task, the jar crated in build/libs/ directory doesn't include JMH's own classes, such as org.openjdk.jmh.Main, so it doesn't seem to me that it generates a fat jar, looks more like a simple jar. OTOH, when I execute the jmh task, the benchmarks are being executed successfully. But I want to execute them via a jar.

Plugin version 0.4.4 Gralde version 4.1

krezisky commented 5 years ago

Recently, I've had the same problem. After adding custom fat jar task to the build.gradle I was able to execute tests via a jar. You have to merge the jmhJar with the jmhRuntime configuration and all other potential deps the runtime requires to run. My case was simple, so above configuration was enough. Try this one, maybe it'll help you either:

task benchmarkJar(type: Jar, dependsOn: jmhJar) {
    manifest {
        attributes 'Main-Class': 'org.openjdk.jmh.Main'
    }
    classifier = 'benchmark'
    from {
        (jmhJar.outputs.files + configurations.jmh + configurations.jmhRuntime)
                .collect { it.isDirectory() ? it : zipTree(it) }
    }
}

To build the benchmark jar execute: ./gradlew benchmarkJar, you will see it at ./build/libs/*-benchmark.jar.

Plugin version: 0.4.7 Gradle version: 4.10.2

gdpotter commented 5 years ago

I've been able to reproduce the issue using the following steps:

git clone https://github.com/melix/jmh-gradle-example.git
cd jmh-gradle-example/
./gradlew jmhJar
java -jar build/libs/custom-1.0-SNAPSHOT-jmh.jar
Error: Could not find or load main class org.openjdk.jmh.Main

I've tested this with many versions of java (8, 9, 10, 11) and all produce the same result. That jmh-gradle-example is the most simple example so I can't figure out what the issue is.

melix commented 5 years ago

To get a fat jar, you need to apply the Shadow plugin. And it must be applied before the JMH plugin is applied. It's not ideal to have an ordering issue here, so it would be good we remove that limitation.