使用KyroNet时NoClassDefFoundError

时间:2015-04-19 18:55:02

标签: java

当我尝试使用kyronet(用于网络)时,我收到此错误。似乎代码中没有任何问题,我将其导出正确。这是日志:

[TextRPG] Starting TextRPG server version: 1.0
[TextRPG] Please ignore any messages by kyronet. This is just the API I used for networking.
[TextRPG] Getting server directory...
[TextRPG] Checking for server properties...
[TextRPG] Properties exists, injecting..
Exception in thread "main" java.lang.NoClassDefFoundError: com/esotericsoftware/kryo/io/Output
at com.esotericsoftware.kryonet.Server.<init>(Server.java:109)
at com.esotericsoftware.kryonet.Server.<init>(Server.java:91)
at dsboy08.TextRPGServer.ServerStartMain.<init>(ServerStartMain.java:34)
at dsboy08.TextRPGServer.ServerStartMain.main(ServerStartMain.java:69)
Caused by: java.lang.ClassNotFoundException:com.esotericsoftware.kryo.io.Output
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more

以下是主类中的代码:

package dsboy08.TextRPGServer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;

import javax.swing.JOptionPane;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;

import dsboy08.TextRPGServer.ConfigVars.ServerProperties;
import dsboy08.TextRPGServer.Packets.Packet;

public class ServerStartMain {

/*
 * Variables & Declaration
 */

static double version = 1.0;
static String currentJarPath;
Server server = new Server();
public int ServerPort;
public int MaxPlayers;
public String ServerName;
public String MOTD;
public boolean serverEnabled = false;

/*
 * Server Start Sequence
 */

public static void main(String[] args) {
    System.out.println("[TextRPG] Starting TextRPG server version: "+version);
    System.out.println("[TextRPG] Please ignore any messages by kyronet. This is just the API I used for networking.");
    System.out.println("[TextRPG] Getting server directory...");
    //Get current jar path
    final Class<?> referenceClass = ServerStartMain.class;
    final URL url =
        referenceClass.getProtectionDomain().getCodeSource().getLocation();
    try{
        final String jarPath = new File(url.toURI()).getParentFile().toString();
        currentJarPath = jarPath;
    } catch(final URISyntaxException e){
        JOptionPane.showMessageDialog(null, "A critical error happended while trying to get the current directory of the jar. Please report this to @3dsboy08.");
        System.exit(-1);
    }
    System.out.println("[TextRPG] Checking for server properties...");
    File f = new File(currentJarPath + "/properties.yml");
    if(f.exists() && !f.isDirectory()) {
        System.out.println("[TextRPG] Properties exists, injecting..");
        //Server config exists. Get it for future reference
        ServerProperties config;
        try {
            YamlReader reader = new YamlReader(new FileReader(currentJarPath+"/properties.yml"));
            config = reader.read(ServerProperties.class);
            new ServerStartMain().readYML(config);
            System.out.println("[TextRPG] Binding to IP: "+getIpAddress());
        } catch (YamlException | FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "A critical error happended while trying to read the servers config file. Please report this to @3dsboy08.");
            System.exit(-1);
        }

    }else{
        System.out.println("[TextRPG] File properties.yml doesn't exist, creating it.");
        try {
            f.createNewFile();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "A critcal error happended while trying to create the servers properties. Please report this to @3dsboy08.");
            System.exit(-1);
        }
        serverSetupWizard();
        //Server setup wizard is done. Get the output.
        try {
            YamlReader reader = new YamlReader(new FileReader(currentJarPath+"/properties.yml"));
            ServerProperties config = reader.read(ServerProperties.class);
            new ServerStartMain().readYML(config);
        } catch (FileNotFoundException | YamlException e) {
            JOptionPane.showMessageDialog(null, "A critical error happended while trying to read the servers config file. Please report this to @3dsboy08.");
            System.exit(-1);
        }
    }

}

/*
 * Server Setup Wizard.
 */

public static void serverSetupWizard(){
    boolean maxPlayersInvalid = true;
    boolean portInvalid = true;
    System.out.println("[TextRPG] Starting server setup wizard.");
    ServerProperties config = new ServerProperties();
    while(maxPlayersInvalid == true){
        String MaxPlayers = JOptionPane.showInputDialog(null, "Please enter in a maximum amount of players for the server.");
        try{
            config.MaxPlayers = Integer.parseInt(MaxPlayers);
            maxPlayersInvalid = false;
            break;
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Error: Invalid amount of players.");
        }
    }
    config.MOTD = JOptionPane.showInputDialog(null, "Please enter in a MOTD for your server.");
    config.ServerName = JOptionPane.showInputDialog(null, "Please enter in a server name for your server.");
    while(portInvalid == true){
        String Port = JOptionPane.showInputDialog(null, "Please enter in a port for your server (it must be a 5-digit number).");
        try{
            if(!(Port.length() == 5)){
                JOptionPane.showMessageDialog(null, "Error: Invalid amount of port characters (must be 5)");
            }else{
                config.ServerPort = Integer.parseInt(Port);
                portInvalid = false;
                break;
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Error: Invalid port number.");
        }
    }
    try {
        YamlWriter writer = new YamlWriter(new FileWriter(currentJarPath+"/properties.yml"));
        writer.write(config);
        writer.close();
        System.out.println("[TextRPG] Successfully created server properties. Please restart the server.");
        System.exit(-1);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "A critical error happended while trying to write to the server properties. Please report this to @3dsboy08.");
        System.exit(-1);
    }
}

/*
 * Read YML
 */
public void readYML(ServerProperties config){
    MOTD = config.MOTD;
    ServerName = config.ServerName;
    ServerPort = config.ServerPort;
    MaxPlayers = config.MaxPlayers;
}

/*
 * Start Server
 */

public void startServer(){
    System.out.println("[TextRPG] Binding packets...");
    server.getKryo().register(Packet.class);
    System.out.println("[TextRPG] Done!");
    server.start();
    try {
        server.bind(ServerPort);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "A critical error happended while trying to bind to a server port (server in use). Please stop the server and try agian.");
    }
    server.addListener(new Listener(){
        public void received (Connection connection, Object object){
            if(object instanceof Packet){

            }
        }
    });

    while(serverEnabled == true){
        Scanner scanner = new Scanner(System.in);
        String commandNotConverted = scanner.nextLine();
        String[] command = commandNotConverted.split(" ");
        if(command[0].equalsIgnoreCase("stop")){
            serverEnabled = false;
            stopServer();
            break;
        }
    }

}

/*
 * Admin commands
 */

public void stopServer(){
    //TODO: Send server closing packet to all players
    System.out.println("[TextRPG] Server closing!");
    server.close();
    System.exit(0);
}


/*
 * Get IP Address 
 */

public static String getIpAddress() 
{ 
        URL myIP;
        try {
            myIP = new URL("http://api.externalip.net/ip/");

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(myIP.openStream())
                    );
            return in.readLine();
        } catch (Exception e) 
        {
            try 
            {
                myIP = new URL("http://myip.dnsomatic.com/");

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(myIP.openStream())
                        );
                return in.readLine();
            } catch (Exception e1) 
            {
                try {
                    myIP = new URL("http://icanhazip.com/");

                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(myIP.openStream())
                            );
                    return in.readLine();
                } catch (Exception e2) {
                    e2.printStackTrace(); 
                }
            }
        }

    return null;
}
}

有什么方法可以解决这个问题吗?如果您有答案,请回复。

编辑:我已经回答了我的问题,并没有重复。问题是我忘了将我的库导出到jar文件。很抱歉给您带来不便。

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。我忘了将所需的库打包到我的jar文件中以导出。对不起,我浪费了人们的时间。虽然这是一个答案,但请记住导出您将要使用的库。