【问题标题】:Exception in thread "main" java.lang.NoClassDefFoundError: when running with console. Possibly Maven-related线程“主”java.lang.NoClassDefFoundError 中的异常:使用控制台运行时。可能与 Maven 相关
【发布时间】:2017-10-18 11:16:34
【问题描述】:

我有 2 个 java 文件:

package com.pokebot;

import javax.security.auth.login.LoginException;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.exceptions.RateLimitedException;

public class App {

    public static void main(String[] args)
    {
        try {
            JDA jdaBot = new JDABuilder(AccountType.BOT).setToken("my_token").buildBlocking();
            jdaBot.addEventListener(new Pokebot());
        } catch (LoginException e) {
    //            System.out.println("LoginException");
        } catch (InterruptedException e) {
    //            System.out.println("InterruptedException");
        } catch (RateLimitedException e) {
    //            System.out.println("RateLimitedException");
        }
    }

}

和:

package com.pokebot;

import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

public class Pokebot extends ListenerAdapter {

    @Override
    public void onMessageReceived(MessageReceivedEvent e) {

        //Obtains properties of the received message
        Message objMsg = e.getMessage();
        MessageChannel objChannel = e.getChannel();
        User objUser = e.getAuthor();

        //Responds to any user who says "hello"
        if (objMsg.getContent().equals("hello")) {
            objChannel.sendMessage("Hello, " + objUser.getAsMention() +"!").queue();
        }

    }
}

当我从 Netbeans 运行应用程序(主类是第一个文件中的 App)时,我只收到一个通知:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

但该应用程序仍然有效。 当我尝试从命令行运行它时:

java -jar target/pokebotapp-1.0-SNAPSHOT.jar

然后我得到一个这样的异常:

Exception in thread "main" java.lang.NoClassDefFoundError: net/dv8tion/jda/core/exceptions/RateLimitedException

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: net/dv8tion/jda/core/
exceptions/RateLimitedException
        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: net.dv8tion.jda.core.exceptions.Rat
eLimitedException
        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

应用程序甚至没有启动。

我在这个项目中使用 Maven。我的 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.pokebot</groupId>
    <artifactId>pokebotapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--<classpathPrefix>lib/</classpathPrefix>-->
                            <mainClass>com.pokebot.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/dependency-jars/
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>3.3.1_291</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
    </repositories>
    </project>

在我尝试从控制台运行项目之前,我运行maven compile。我认为这可能是我运行程序的方式的错误,或者我为这个项目设置 maven 的方式。有没有人知道可能是什么问题?

【问题讨论】:

  • 我为 slf4j-api-1.7.5slf4j-simple-1.7.5 添加了 maven 依赖项,但仍然有同样的问题,即使在 mvn clean install 之后
  • 您是否更新了项目并检查了强制更新快照和版本?
  • 我试过 mvn clean install -U 和 Netbeans 的 Download declared dependencies... 还是一样。有没有其他方法可以更新项目?
  • 右击你的项目->Maven->更新项目->勾选强制更新快照和发布,然后点击确定

标签: java maven netbeans command-line-interface


【解决方案1】:

那是因为 java 不知道从哪里获取这些库。几个选项:

  1. 构建包含所有依赖项的 jar(所谓的 uber-jar)。这是通过 maven shade 插件完成的。文档可在此处获得:https://maven.apache.org/components/plugins/maven-shade-plugin/examples/executable-jar.html

  2. 执行 java -jar -cp 文档在这里:http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

第一个选项肯定更好:) 我想应该有很多关于 stackoverflow 如何配置 maven-shade-plugin 的示例。像这样: What is the maven-shade-plugin used for, and why would you want to relocate java packages?

【讨论】:

  • 只需添加 maven 插件即可解决问题。谢谢:)
猜你喜欢
  • 2014-03-20
  • 1970-01-01
  • 2018-12-06
  • 2011-09-14
  • 2014-11-06
  • 2012-11-18
  • 1970-01-01
相关资源
最近更新 更多