导出为Runnable Jar文件,java执行错误

时间:2017-08-17 08:48:46

标签: java eclipse jar compilation

使用已经完成的项目,希望它能够从单个.jar文件进行编译和运行,正如其他提到的那样,我应该使用Export - > Runnable Jar文件。

我选择我的主要Server类,同时选择“将所需的库打包到生成的JAR中”并按Finish。

现在我有了一个名为Server.jar

的文件

尝试从控制台运行:java -jar Server.jar并收到错误:

java.lang.NullPointerException
at server.Server.main(Server.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

指的是:

Socket socket = serverSocket.accept();

MANIFEST.MF文件(自动创建):

Manifest-Version: 1.0
Rsrc-Class-Path: ./ json-simple.jar mysql-connector-java-5.1.42-bin.ja
 r
Class-Path: .
Rsrc-Main-Class: server.Server
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

我厌倦了猜测问题,我是Java新手,首先进行编译。有什么帮助吗?

我的Server.java类:

package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import org.json.simple.parser.*;


public class Server {

    public static void main(String args[]) throws ParseException{

        ServerSocket serverSocket = null;
        ArrayList<ClientComm> clients = new ArrayList<ClientComm>();

        try{
                new Config();
            serverSocket = new ServerSocket(Config.getPort()); // can also use static final PORT_NUM , when defined
            System.out.println("Server started");
            System.out.println("test");
        }
        catch(IOException e){
            e.printStackTrace();
            System.out.println("Server error");
        }

        int clientID = 0;

        while(true){
            try{
                    System.out.println("Waiting for new connection");
                Socket socket = serverSocket.accept();
                clientID++;
                System.out.println("New connection on socket: " + socket);
                ClientComm clientComm = new ClientComm(socket, clientID);
                clients.add(clientComm);

                if(clientComm.checkAuth() == false) {
                        System.out.println("Client was dropped from connection because auth codes did not match");
                        socket.close();
                }
                else {
                        clientComm.start();
                }  
            }
            catch(Exception e){
                e.printStackTrace();
                System.out.println("Connection Error");
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

问题早已解决。我个人不好,缺乏使用Java编程的经验。