Я использую JRAW (A Java Reddit API) и JDA (Java Discord API) с Maven, чтобы создать диск-бот для сервера. Когда я запускаю свой код в моей IDE (IntelliJ IDEA), он работает как задумано, хотя, если я пытаюсь сжать и запустить код в артефакте (точнее, в файле .jar), он генерирует исключение ClassNotFoundException (kotlin .Pair) .
Журнал:
java.lang.NoClassDefFoundError: kotlin/Pair
at net.dean.jraw.oauth.OAuthHelper.scriptOAuthData$lib(OAuthHelper.kt:60)
at net.dean.jraw.oauth.OAuthHelper.automatic(OAuthHelper.kt:32)
at net.dean.jraw.oauth.OAuthHelper.automatic$default(OAuthHelper.kt:27)
at net.dean.jraw.oauth.OAuthHelper.automatic(OAuthHelper.kt)
at javaCode.Tools.Reddit.<clinit>(Reddit.java:22)
at javaCode.Listeners.CommandListener.onGuildMessageReceived(CommandListener.java:205)
at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:345)
at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:64)
at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:152)
at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:97)
at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:37)
at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:843)
at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:739)
at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:718)
at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:881)
at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:368)
at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:270)
at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:990)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:749)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
Caused by: java.lang.ClassNotFoundException: kotlin.Pair
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 23 more
POM. xml Файл:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<packaging>jar</packaging>
<groupId>1.0.0</groupId>
<artifactId>TDCBot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<kotlin.version>1.3.70</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.jflac</groupId>
<artifactId>jflac-codec</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.7.1_386</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.5</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>1.3.70</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.3.70</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Reddit Код класса:
package javaCode.Tools;
import net.dean.jraw.RedditClient;
import net.dean.jraw.http.NetworkAdapter;
import net.dean.jraw.http.OkHttpNetworkAdapter;
import net.dean.jraw.http.UserAgent;
import net.dean.jraw.models.Listing;
import net.dean.jraw.models.Submission;
import net.dean.jraw.models.TimePeriod;
import net.dean.jraw.oauth.Credentials;
import net.dean.jraw.oauth.OAuthHelper;
import net.dean.jraw.pagination.DefaultPaginator;
import net.dv8tion.jda.api.EmbedBuilder;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
public class Reddit {
private static final Credentials credentials = Credentials.script("[USERNAME]", "[password]", "
[CLIENT_ID]", "[CLIENT_SECRET]");
private static final UserAgent agent = new UserAgent("bot", "javaCode.Tools.Reddit", "v1.0",
"Bot");
private static final NetworkAdapter adapter = new OkHttpNetworkAdapter(agent);
private static final RedditClient client = OAuthHelper.automatic(adapter, credentials);
private static int prev = 0; //Saves the previous post index.
/**
* Returns an embed with a random meme from r/dankmemes or r/memes.
*/
public static EmbedBuilder getRandomMemeEmbed() {
EmbedBuilder builder = new EmbedBuilder();
DefaultPaginator<Submission> paginator = client.subreddits("dankmemes", "memes").posts()
.timePeriod(TimePeriod.MONTH) //Only memes posted this week.
.limit(100)
.build();
Listing<Submission> page = paginator.next(); //Gets a page filled with reddit posts.
int randomNum = ThreadLocalRandom.current().nextInt(0, page.size()); //Generates a randomNumber.
if (randomNum == prev) {
randomNum = ThreadLocalRandom.current().nextInt(0, page.size());
}
prev = randomNum;
Collections.shuffle(page); //randomises the list.
builder.setTitle(page.get(randomNum).getTitle());
if (page.get(randomNum).getThumbnail() != null) { //If the meme has got an image.
builder.setImage(page.get(randomNum).getThumbnail());
} else {
getRandomMemeEmbed(); //Find another one.
}
return builder;
}
}
Надеюсь, кто-нибудь может мне помочь с этим. Благодаря.