我有 nio channel ,我的客户端应该从服务器计算机加载类文件。它们的IP范围相同。我有两个在服务器和客户端计算机上常见的接口(interface)。以及在服务器计算机上实现接口(interface)的类。我在我的客户端机器上使用以下代码,但是当我运行它时会出现ClassNotFoundException。
URL url = new URL("file:///E:/Computing/Master/classes/" );
URLClassLoader ucl = new URLClassLoader(new URL[]{url});
Class clazz = ucl.loadClass("com.counter.controller.Action");
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance();
本例中类加载的完整流程是怎样的?
最佳答案
我找到了解决方案并想在这里分享。首先,这个工作是网络类加载。可以在 javadoc 中找到该名称。 实际上,无法使用以下代码从远程计算机加载类文件:
URL url = new URL("file:///E:/Computing/Master/classes/" );
URLClassLoader ucl = new URLClassLoader(new URL[]{url});
Class clazz = ucl.loadClass("com.counter.controller.Action");
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance();
即使您将其 URL 更改为“http”,而两台独立计算机之间没有 http 协议(protocol)。好吧,让我们以正确的方式开始吧。
假设您有两台 IP 为 192.168.10.1(服务器)和 192.168.10.2(客户端)的计算机。有一个类文件,客户端不应将其从服务器磁盘复制到其磁盘。因此,首先,开始在 JVM(服务器和客户端)上定义相同的接口(interface)。具有与以下接口(interface)相同的包:
package org.counter.biz;
public interface ProcessAlgorithm {
int doProcess() ;
}
所以,这个接口(interface)在服务器和客户端上是通用的。其次,您的主类应该在服务器上定义并实现接口(interface):
package org.counter.biz;
public class Action implements ProcessAlgorithm {
@Override
public int doProcess() {
/* something to do */
}
}
最后,类文件应该通过套接字或套接字 channel 发送到客户端。在这里,我在服务器上使用 Socketchannel,在客户端上使用 Socket。 (实际上,你首先要知道如何通过套接字连接两台远程计算机。)
将类文件字节发送到客户端的服务器端代码:
private void sendAlgorithmFile(SocketChannel client, String filePath) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(8192);
buffer.clear();
/*file path like E:\\classes\\Action.class*/
Path path = Paths.get(filePath);
FileChannel fileChannel = FileChannel.open(path);
int bytes = 0;
int counter = 0;
do {
bytes = fileChannel.read(buffer);
if (bytes <= 0)
break;
counter += bytes;
buffer.flip();
do {
bytes -= client.write(buffer);
} while (bytes > 0);
buffer.clear();
} while (true);
fileChannel.close();
}
有很多方法可以通过套接字发送文件。这是我的代码,其正确性已经过检查。
客户端接收文件并将其更改为未保存在客户端磁盘上的类。
package org.counter.biz;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class MyClassLoader extends ClassLoader {
private Socket clientChannel;
private int count = 0;
public MyClassLoader(Socket channel){
this.clientChannel = channel;
}
@Override
protected Class findClass(String className){
Class myClass = null;
InputStream inputStream = null;
try {
inputStream = clientChannel.getInputStream();
}catch (IOException e){e.printStackTrace();}
byte[] bytes = new byte[8192];
byte[] myBytes = null;
try {
while ((count = inputStream.read(bytes)) > 0){
myBytes = new byte[count];
System.arraycopy(bytes, 0, myBytes, 0, count);
myClass = defineClass(className, myBytes, 0, myBytes.length);
}
inputStream.close();
}catch (IOException io){}
return myClass;
}
}
然后:
public class Client {
public static void main(String[] args) throws Exception{
MyClassLoader myClassLoader = new MyClassLoader(clientSocket);
Class clazz = myClassLoader.findClass(null);
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance();
}
}
然后你可以像这样使用该类
iAction.doProcess();
如果有任何问题,我会在这里回答。 :)
关于java - 如何使用类加载器从服务器加载类文件到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41963273/