有一个类MyObj,类文件是在一个jar包中,然后jar包位置在一个HTTP服务器上,我是用 URLClassLoader 通过HTTP协议自己装入的,打算加载后进行实例化。客户端的代码如下:
String urlStr = "jar:http://localhost:8081/lesavon/getRemoteJar?jarName=service.jar!/";
String className = "com.lesavon.service.impl.OrderService";URL url = new URL(urlStr);
URL[] urls = new URL[]{ url };
URLClassLoader ucl =
new URLClassLoader(urls, this.getClass().getClassLoader());
Class cls = ucl.loadClass(className);
服务端的代码
private void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String jarName = (String)req.getParameter("jarName");
if (isNullOrSpace(jarName)) {
//请求参数为空错误。。
}
BufferedInputStream bis = null;
//获得service lib目录:
String javaHomeStr = "D:\\myeclipse-workspace\\lesavon\\WEB-INF";
String jarDirPath = javaHomeStr + "\\lib\\";
String jarFilePath = jarDirPath + jarName;
//将jar包写出response流:
resp.addHeader("Content-Disposition", "attachment;filename=" + jarName);
resp.setHeader("Connection", "close");
resp.setHeader("Content-Type", "application/java-archive");
bis = new BufferedInputStream(
new FileInputStream(jarFilePath));
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
byte[] bytes = new byte[1024];
int i = 0;
while ((i=bis.read(bytes, 0, 1024))!=-1) {
bos.write(bytes);
} bos.flush();
bos.close();
bis.close();
}最后客户端报错,说是找不到指定的类。服务端我直接用http://localhost:8081/lesavon/getRemoteJar?jarName=service.jar在浏览器测试过,没有问题,可以跳出下载框提示下载jar包。那么客户端为什么加载不到服务端jar包里的类呢?先在这里谢谢了~
String urlStr = "jar:http://localhost:8081/lesavon/getRemoteJar?jarName=service.jar!/";
String className = "com.lesavon.service.impl.OrderService";URL url = new URL(urlStr);
URL[] urls = new URL[]{ url };
URLClassLoader ucl =
new URLClassLoader(urls, this.getClass().getClassLoader());
Class cls = ucl.loadClass(className);
服务端的代码
private void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String jarName = (String)req.getParameter("jarName");
if (isNullOrSpace(jarName)) {
//请求参数为空错误。。
}
BufferedInputStream bis = null;
//获得service lib目录:
String javaHomeStr = "D:\\myeclipse-workspace\\lesavon\\WEB-INF";
String jarDirPath = javaHomeStr + "\\lib\\";
String jarFilePath = jarDirPath + jarName;
//将jar包写出response流:
resp.addHeader("Content-Disposition", "attachment;filename=" + jarName);
resp.setHeader("Connection", "close");
resp.setHeader("Content-Type", "application/java-archive");
bis = new BufferedInputStream(
new FileInputStream(jarFilePath));
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
byte[] bytes = new byte[1024];
int i = 0;
while ((i=bis.read(bytes, 0, 1024))!=-1) {
bos.write(bytes);
} bos.flush();
bos.close();
bis.close();
}最后客户端报错,说是找不到指定的类。服务端我直接用http://localhost:8081/lesavon/getRemoteJar?jarName=service.jar在浏览器测试过,没有问题,可以跳出下载框提示下载jar包。那么客户端为什么加载不到服务端jar包里的类呢?先在这里谢谢了~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货