Solved External Libraries in my plugin

Discussion in 'Spigot Plugin Development' started by you_kill, May 14, 2022.

  1. Hello, I'm creating a plugin that send a mail to the players ; my IDE is IntelliJ IDEA.
    I've download javax.mail and smtp to send the mails, I add them with the IDE as Librairies and I put them in the Artifacts.
    But when I start my plugin I have an error :

    java.lang.NoClassDefFoundError: javax/mail/Authenticator
    at roumite.ville.main.onEnable(main.java:10) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugin(CraftServer.java:518) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugins(CraftServer.java:432) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.loadWorld0(MinecraftServer.java:612) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.loadLevel(MinecraftServer.java:414) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:263) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1007) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at java.lang.Thread.run(Thread.java:833) [?:?]
    Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
    at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:147) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:99) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]

    I read on the net that is an error about the javapath, but I don't know how to solve them...
    tell me if you want the code, but I don't think that is usefull
    Thank you for the help
    -Julien SERIN
     
  2. If you're using Maven you have to shade these libs in your project using maven-shade-plugin.
     
  3. how can I do that ? Have you got an example ?
    How I install that plugin ?
     
  4. Yep, here you go!
    Idk if this will help you
    HTML:
    <?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>me.example.group</groupId>
        <artifactId>ExampleShadePluginByYoshiro09</artifactId>
        <version>1.0-SNAPSHOT</version>

        <properties>
            <maven.compiler.source>16</maven.compiler.source>
            <maven.compiler.target>16</maven.compiler.target>
        </properties>

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory> <!-- (your resources folder location) -->
                    <filtering>true</filtering> <!-- this is the important part, it's what replaces, filters, all placeholders in the resources folder (such as ${project.version} in plugin.yml) -->
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.4</version>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <!-- Excludes these libs from being shaded in the project -->
                                <!-- Format: <exclude>put.here.the.group.id:put.here.the.version</exclude> -->
                                <!-- In this example, only Spigot won't be shaded in the project -->
                                <exclude>org.spigotmc:1.18-R0.1-SNAPSHOT</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <!-- This adds the Spigot Maven repository to the build -->
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>

        <dependencies>
            <!--This adds the Spigot API artifact to the build -->
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.18-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>

    </project>
     
  5. I'm sorry I really don't anderstand...
    Where I put that ?
    That my stucture
    PS: the file named maven is nothing lmao
     

    Attached Files:

    #5 you_kill, May 14, 2022
    Last edited: May 14, 2022
  6. If your dependencies live in Maven Central, then you can use Bukkit's plugin.yml library api.
     
  7. Ok, i've create a new projetct using Maven, but I have the same error lmao
    Here is my 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>fr.youkill</groupId>
    <artifactId>pluginVille</artifactId>
    <version>1.0</version>

    <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <repositories>
    <repository>
    <id>spigot-repo</id>
    <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
    </repositories>

    <dependencies>
    <dependency>
    <groupId>org.spigotmc</groupId>
    <artifactId>spigot-api</artifactId>
    <version>1.18.2-R0.1-SNAPSHOT</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>jakarta.mail</groupId>
    <artifactId>jakarta.mail-api</artifactId>
    <version>2.1.0</version>
    </dependency>
    </dependencies>
    </project>

    I've add my dependency jakarta.mail but when I start my server I have this error :/

    java.lang.NoClassDefFoundError: jakarta/mail/Authenticator
    at fr.youkill.pluginVille.main.onEnable(main.java:10) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugin(CraftServer.java:518) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugins(CraftServer.java:432) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.loadWorld0(MinecraftServer.java:612) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.loadLevel(MinecraftServer.java:414) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:263) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1007) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3486-Spigot-b6d12d1-f3c7a6a]
    at java.lang.Thread.run(Thread.java:833) [?:?]
    Caused by: java.lang.ClassNotFoundException: jakarta.mail.Authenticator
    at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:147) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:99) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]
    ... 12 more

    Thank you all for your answer.
     
  8. If you want to do it like Jannyboy said I recommend you to check the 'Libraries' section in the wiki (plugin.yml).
    If you want to do it using maven-shade-plugin, you have to put at least this in your pom.xml file.
    You should take a look at this wiki (maven-shade-plugin) too and at its usage page.
    HTML:
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.4</version>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <!-- Excludes these libs from being shaded in the project -->
                                <!-- Format: <exclude>put.here.the.group.id:put.here.the.version</exclude> -->
                                <!-- In this example, only Spigot won't be shaded in the project -->
                                <exclude>org.spigotmc:1.18-R0.1-SNAPSHOT</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
     
  9. It work ! Thank you sooooooo much <3