首页 > 解决方案 > Exception in thread "main" java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir

问题描述

This is my first time working with Maven and I'm attempting to set up this project. First, I downloaded Maven 3.6.1 and set it up (JDK version: 1.8.0). When setting up, since the README.md directions seem to be wrong, I cloned the repo and first ran:

mvn clean

then I ran:

mvn install

after that, I ran:

mvn clean compile

Then to execute the jar I ran the following command:

java -cp target/classes org.wso2.adaptive_concurrency_control.AdaptiveConcurrencyControl

When I run the above command I keep getting this error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: com.codahale.metrics.Reservoir at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 7 more

The pom.xml looks like this:

<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.nilushan</groupId>
    <artifactId>adaptive-concurrency-control</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>adaptive_concurrency_control</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>jcenter</id>
            <url>https://jcenter.bintray.com/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
            <version>4.1.22.Final</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-jmx</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-graphite</artifactId>
            <version>3.0.2</version>
        </dependency> -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--<plugin>-->
                <!--<groupId>org.apache.maven.plugins</groupId>-->
                <!--<artifactId>maven-jar-plugin</artifactId>-->
                <!--<version>3.1.0</version>-->
                <!--<configuration>-->
                    <!--<archive>-->
                        <!--<manifest>-->
                            <!--<addClasspath>true</addClasspath>-->
                            <!--<mainClass>AdaptiveConcurrencyControl</mainClass>-->
                        <!--</manifest>-->
                    <!--</archive>-->
                <!--</configuration>-->
            <!--</plugin>-->
            <!--<plugin>-->
                <!--<artifactId>maven-assembly-plugin</artifactId>-->
                <!--<configuration>-->
                    <!--<archive>-->
                        <!--<manifest>-->
                            <!--<mainClass>AdaptiveConcurrencyControl</mainClass>-->
                        <!--</manifest>-->
                    <!--</archive>-->
                    <!--<descriptorRefs>-->
                        <!--<descriptorRef>jar-with-dependencies</descriptorRef>-->
                    <!--</descriptorRefs>-->
                <!--</configuration>-->
            <!--</plugin>-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.wso2.adaptive_concurrency_control.AdaptiveConcurrencyControl</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

When I run:

mvn dependency:tree

I get this:

[INFO] com.nilushan:adaptive-concurrency-control:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- io.netty:netty-all:jar:4.1.22.Final:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.11.1:compile
[INFO] |  \- org.apache.logging.log4j:log4j-api:jar:2.11.1:compile
[INFO] +- io.dropwizard.metrics:metrics-core:jar:4.0.0:compile
[INFO] +- io.dropwizard.metrics:metrics-jmx:jar:4.0.0:compile
[INFO] \- mysql:mysql-connector-java:jar:5.1.47:compile

I cannot figure out what the problem is. Any help is much appreciated.

标签: javamaven-3dropwizardmetrics

解决方案


您现在使用的是较新版本的 Codahale 指标,它是 dropwizard。包结构发生了变化。要解决您的问题,您必须使用以下依赖项。

    <!-- https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core -->
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.codahale.metrics/metrics-graphite -->
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-graphite</artifactId>
    <version>3.0.0</version>
</dependency>

您可以从链接中获取所有详细信息。

https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core/3.0.2

要了解 jar 文件的详细信息,您可以参考以下内容并找到该类。

http://www.java2s.com/Code/Jar/m/Downloadmetricscore300jar.htm


推荐阅读