[英]How to use classloader to load class file from server to client
我有nio通道,我的客户端应该从服务器计算机加载类文件。 它们属于相同的IP范围。 我有两个在服务器和客户端计算机上通用的接口。 和一个在服务器计算机上实现接口的类。 我在客户端计算机上使用以下代码,但是在运行它时会出现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();
即使在两台单独的计算机之间没有http协议的情况下,将其URL更改为“ http”。 好吧,让我们开始正确的方法。
假设您有两台IP地址为192.168.10.1(服务器)和192.168.10.2(客户端)的计算机。 有一个类文件,客户端不应将其从服务器磁盘复制到其磁盘。 因此,首先,开始在JVM(服务器和客户端)上定义相同的接口。 具有与以下界面相同的软件包:
package org.counter.biz;
public interface ProcessAlgorithm {
int doProcess() ;
}
因此,此接口在服务器和客户端上很常见。 其次,您的主类应在服务器上定义并实现接口:
package org.counter.biz;
public class Action implements ProcessAlgorithm {
@Override
public int doProcess() {
/* something to do */
}
}
最后,应将类文件通过套接字或套接字通道发送到客户端。 在这里,我在服务器上使用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();
如果有任何问题,我在这里回答。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.