URL类加载器找不到我的类Java

时间:2018-06-20 05:19:45

标签: java serversocket urlclassloader

我已经花了几个小时编写关于如何通过网络加载类的代码,尝试了我可以提供的所有解决方案,但是错误仍然存​​在,需要有关如何找到我的Adder类的帮助。谢谢

这是我的客户代码:

public class Client {
    public final String ipadd = "192.168.1.5";
    BufferedReader in;
    PrintWriter out;
    public static void main(String[] args) throws Exception {
        Client client = new Client();
        client.run();
    }


    private void run() throws IOException {

        // Make connection and initialize streams
        String serverAddress = ipadd;


        File cFile = null;
        URL url = null;
        URL[] urls = null;

        String URLadd = null,classToLoad = null,methodToUse = null;

        // Connecting to server
        Socket socket = new Socket(serverAddress, 9002);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
            socket.getInputStream()));



        // Do stuff

        // keep reading line until reaches null
        for(int i = 0; i <= 2 ; i ++){
            String input = in.readLine();
            if(input.startsWith("URL: ")){
                //get URL from this line
                URLadd = input.substring(11);
                //System.out.println(URLadd);
                cFile = new File(URLadd);
                url = cFile.toURI().toURL();
            }else if (input.startsWith("Class: ")) {

                //get Class from this line
                classToLoad = input.substring(7);
                //System.out.println("HaLLLERR"+classToLoad);
            }else if(input.startsWith("Method: ")){
                //get Method from this line
                methodToUse = input.substring(8);
                //System.out.println(methodToUse);
            }
        }



        //Load Class
        System.out.println("URL Address: " + URLadd);
        System.out.println("Class: " + classToLoad);
        System.out.println("Method: " + methodToUse);

        urls = new URL[]{url};
        System.out.println(Arrays.toString(urls));
        ClassLoader cl = new URLClassLoader(urls);

        //Class temp = loadClass(URLadd,classToLoad);


        try {
            Class temp = cl.loadClass(classToLoad);
            System.out.println("DONE LOADING CLASS");
            System.out.println(temp);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }


        // Do what is asked by server


    }

}

服务器代码:

public class Server {
    private static final int PORT = 9002;

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

        System.out.println("The server is running.");
        ServerSocket listener = new ServerSocket(PORT);
        try {
            while (true) {
                new Server.Handler(listener.accept()).start();
            }
        } finally {
            listener.close();
        }

//        Server temp = new Server();
//        temp.sendURL();


    }

    private static class Handler extends Thread {
        private String name;
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;
        Server temp = new Server();

        public Handler(Socket accept){
            this.socket = accept;
        }


        public void run(){
            try {
                if(socket!=null){
                    System.out.println(socket);
                    in = new BufferedReader(new InputStreamReader(
                            socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream(), true);


                    // send URL
                    out.println("URL: " + (temp.sendURL()));
                    System.out.println("URL: " + (temp.sendURL()));
                    // send what Class to load (example: com.mycompany.MyClass)
                    out.println(temp.sendClass("Adder"));
                    System.out.println(temp.sendClass("Adder"));
                    // send What method to do
                    out.println(Server.sendMethod("add"));
                    System.out.println(Server.sendMethod("add"));
                }else{
                    System.out.println("Socket is null!");
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static URL sendURL (){
        // Create a File object on the root of the directory containing the class file
        File file = new File("F:/Music/Library/NetBeansProjects/Java 3 way chat/src/Client/");
        //File file = new File("F:/Music/Library/NetBeansProjects/Java_3_way_chat/dist/Java_3_way_chat.jar");
        URL url = null;
        //URL[] urls = null;
        try {
            // Convert File to a URL
            url = file.toURI().toURL();          // F:/Music/Library/NetBeansProjects/Java 3 way chat/dist/Java_3_way_chat.jar
            //urls = new URL[]{url};
        } catch (MalformedURLException e) {
        }finally{
            return url;
        }
    }

    public static String sendClass(String temp){
        return ("Class: " + temp);
    }

    public static String sendMethod(String method){
        return ("Method: " + method);
    }


}

我的Adder类已经在正确的文件路径中,需要帮助为什么URLClassLoader无法找到我的Adder类Project Outlook

0 个答案:

没有答案