Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - How do I use an API for my Bukkit plugins?

As the title says I try to make a Bukkit Plugin for my Minecraft 1.16.1 Spigot Server which uses a API. My plan was to make a plugin (in Eclipse) which analyses some statistic of the server such as playercount and TPS and so on and send it via email to my email-address. To achieve that I need to implement Java-EE because Java SE does not have the ability to send emails. Furthermore I need to implement the java-mail-api. In total there are 4 .jar files which work perfectly fine when I am running it as a normal jar file with the 4 .jars set in the buildpath as "use external jars". It also workes perfectly fine when I use maven to implement the APIs as dependencies into the pom.xml. No problem. But things start to go wrong when I implement the working code into a bukkit plugin.

At first I setup a simple onEnable method which looks like that

package mailsend;

import org.bukkit.plugin.java.JavaPlugin;

public class Mailer extends JavaPlugin {
    
    Commands cmds;
    
    @Override
    public void onEnable() {

        cmds = new Commands(); // this is line 12
        
        this.getCommand(cmds.cmd).setExecutor(cmds);
        
        System.out.println("Enabled Plugin MAIL");
        
    
    }
    
}

It enables the plugin and the command executor which looks like that

package mailsend;

import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Commands implements CommandExecutor {

    String cmd = "send";
    
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        Player p = null;
        
        if(sender instanceof Player)
            p = (Player) sender;
        
        if(command.getName().equals(cmd)) {

            MailSender sndr = new MailSender();

            sndr.login(Data.smtp, Data.number, Data.email, Data.password);

            sndr.sendTestMail();
            
            return true;
        }
        
        return false;
    }
    
}

The problem occurs when I reload my server. The exception is

[19:42:15 INFO]: [Mailer] Enabling Mailer v1.0
[19:42:15 ERROR]: Error occurred while enabling Mailer v1.0 (Is it up to date?)
java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at mailsend.Mailer.onEnable(Mailer.java:12) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugin(CraftServer.java:491) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugins(CraftServer.java:405) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at net.minecraft.server.v1_16_R1.MinecraftServer.loadWorld(MinecraftServer.java:438) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at net.minecraft.server.v1_16_R1.DedicatedServer.init(DedicatedServer.java:219) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:810) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_271]
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_271]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:167) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:96) ~[Spigot.jar:git-Spigot-ad703da-e2403a3]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_271]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_271]
    ... 11 more

All I know about this exception is that the class from the API I am using could not be loaded in this case javax/mail/MessagingException. That means that my plugin does not use the APIs I set into the buildpath right? I tried export the plugin with the APIs implemented but that does not work. I also tried using maven for this Bukkit plugin buth that also does not work. I always gete this exception and I have literally no idea how to fix this. But as I said I guess that my plugin is not getting exported the right way with the APIs included or the link to the APIs (I also made a folder called "lib" where I put the APIs in, did not work..). I also tried to add the classpaths into the MANIFEST file which is generated in the plugin but that does not work either..

I am personally making a plugin with the usage of APIs the first time and I really have no idea how to do this the right way. If someone could give me simple instructions on how to solve this problem, it would be really really great!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is your server running on the OpenJDK? To my knowledge for OpenJDK does not contain the javax. classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...