使用applet处理.dll文件loading/unloading issue。我正在使用自定义类加载器加载this tutorial,。dll文件:
1-级加载程序(从tutorial复制)
public class CustomClassLoader extends ClassLoader {
private static final String CLASS_NAME = CustomClassLoader.class.getName();
private Map<String, Class<?>> classes;
public CustomClassLoader() {
super(CustomClassLoader.class.getClassLoader());
classes = new HashMap<String, Class<?>>();
}
public String toString() {
return CustomClassLoader.class.getName();
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
if (classes.containsKey(name)) {
return classes.get(name);
}
String path = name.replace('.', File.separatorChar) + ".class";
byte[] b = null;
try {
b = loadClassData(path);
} catch (IOException e) {
throw new ClassNotFoundException("Class not found at path: " + new File(name).getAbsolutePath(), e); **
}
Class<?> c = defineClass(name, b, 0, b.length);
resolveClass(c);
classes.put(name, c);
return c;
}
private byte[] loadClassData(String name) throws IOException {
String methodName = CLASS_NAME + ".loadClassData";
byte buff[] = null;
try {
File file = new File(name);
int size = (int) file.length();
buff = new byte[size];
DataInputStream in = new DataInputStream(new FileInputStream(name));
in.readFully(buff);
in.close();
} catch (IOException e) {
throw new IOException(e.getMessage());
}
return buff;
}
}
2-加载.dll文件的类
public class DllLoader {
private static final String CLASS_NAME = AppletReload.class.getName();
static String javaHome = System.getProperty("java.io.tmpdir");
private static String dllPath = javaHome + Constant.SMARTCARD_JACSPCSC_DLL_NAME;
private static String dllPath1 = javaHome + Constant.SMARTCARD_RXTXSERIAL_DLL_NAME;
public DllLoader() {
}
static {
try {
System.load(dllPath);
Logger.write(LoggerConstant.TRACE, "JACSPCSC Dll loaded from the path: " + dllPath, "Dll Loader");
System.load(dllPath1);
Logger.write(LoggerConstant.TRACE, "RXTXSERIAL Dll loaded from the path: " + dllPath1, "Dll Loader");
} catch (Exception e) {
// Log exception;
}
}
}
这就是我使用这个类加载器的方式:
cl = new CustomClassLoader();
ca = cl.findClass("com.DllLoader");
a = ca.newInstance();
使用自定义类加载器加载.dll背后的动机是它也能保证卸载,这就是.dll加载/卸载提出的大多数问题的答案。但就我而言,loadClassData()
(我的classLoader中的一个函数)引发了这个异常:
loadClassData,Error: com\DllLoader.class (The system cannot find the path specified)
java.io.FileNotFoundException: com\DllLoader.class (The system cannot find the path specified)
并且文件的绝对路径显示为:
** C:\ Program Files \ Mozilla Firefox \ com.DllLoader
我认为这是搜索文件的位置,applet的.jar文件不在此处。
如果有人能指出我正在制作的错误或者告诉我如何指导浏览器在正确的文件夹中查找类文件,我将不胜感激。
P.S:请注意,似乎duplicate question的答案并不能解决这个问题。