Search code examples
mavenintellij-ideajava-8jmhopenscience-cdk

DefaultChemObjectBuilder ClassNotFoundException occur when running jmh benchmark in terminal Intellij Idea


I can run the project as an application in IntelliJ IDEA by using main method. But when I'm trying to run it using terminal for benchmark, a class not found exception occurs.

public class MyBenchmark {

    @Benchmark
    public static void sdfIterativeReader() throws ClassNotFoundException, FileNotFoundException {
        File sdfFile = new File("molecule.sdf");
        IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();

        IteratingSDFReader reader = new IteratingSDFReader(new FileInputStream(sdfFile),builder);
        while (reader.hasNext()) {
            IAtomContainer molecule = (IAtomContainer) reader.next();
            IMolecularFormula formula = MolecularFormulaManipulator.getMolecularFormula(molecule);
            String molecularFromula = MolecularFormulaManipulator.getString(formula);
            System.out.println(molecularFromula);
        }
    }
}

I used the command: mvn clean install then project is building successfully. When I run the command:

java -jar target/benchmarks.jar

it gives the following error:

enter image description here

Here, I have attached my pom.xml:

<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.chemid</groupId>
<artifactId>chemid-benchmark</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>JMH benchmark sample: Java</name>
<prerequisites>
    <maven>3.0</maven>
</prerequisites>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
    <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>
    <dependency>
        <groupId>cdk</groupId>
        <artifactId>cdk</artifactId>
        <version>1.5.13</version>
        <scope>system</scope>
        <systemPath>${cdk.lib}/cdk-1.5.13 .jar</systemPath>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.streamsupport</groupId>
        <artifactId>streamsupport</artifactId>
        <version>1.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.openscience.cdk</groupId>
        <artifactId>cdk-io</artifactId>
        <version>1.5.13</version>
    </dependency>
    <dependency>
        <groupId>org.openscience.cdk</groupId>
        <artifactId>cdk</artifactId>
        <version>1.5.13</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.openscience.cdk</groupId>
        <artifactId>cdk-core</artifactId>
        <version>1.5.13</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <jmh.version>1.6.3</jmh.version>
    <javac.target>1.8</javac.target>
    <uberjar.name>benchmarks</uberjar.name>
    <cdk.lib>${basedir}/libs</cdk.lib>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <compilerVersion>${javac.target}</compilerVersion>
                <source>${javac.target}</source>
                <target>${javac.target}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${uberjar.name}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.openjdk.jmh.Main</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <!--
                                    Shading signed JARs will fail without this.
                                    http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                                -->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Solution

  • Add to file MANIFEST.MF this line:

    Class-Path: lib/cdk-1.5.13.jar

    Or run you benchmark thought class with main method:

    java -cp path/to/cdk/lib/cdk-1.5.13.jar:. com.your.main.ClassName

    For more info read answers from this discussion.

    And also this answer to understand system scope of Maven dependency that you've chose for cdk-1.5.13.jar.