由于操作系统限制,文件后缀名只能为3位,如何让虚拟机找到3位后缀名的类?

bhjsj 2005-12-21 04:00:58
我想象中这个应该自己写类加载器来实现,这种思路是否可以解决这个问题?有做过这方面需求的大侠来肯定一下吗?
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
whyxx 2005-12-21
  • 打赏
  • 举报
回复
完全是可以的,以下是在common.FileSystemClassLoader实现的主方法:
请注意String pathName = currentRoot + File.separatorChar+ className.replace('.', File.separatorChar) + ".class";这句话.

public byte[] findClassBytes(String className) {
try {
String pathName = currentRoot + File.separatorChar
+ className.replace('.', File.separatorChar)
+ ".class";
FileInputStream inFile = new FileInputStream(pathName);
byte[] classBytes = new byte[inFile.available()];
inFile.read(classBytes);
return classBytes;
} catch (java.io.IOException ioEx) {
return null;
}
}

public Class findClass(String name) throws ClassNotFoundException {
byte[] classBytes = findClassBytes(name);
if (classBytes == null) {
throw new ClassNotFoundException();
} else {
return defineClass(name, classBytes, 0, classBytes.length);
}
}

public Class findClass(String name, byte[] classBytes)
throws ClassNotFoundException {
if (classBytes == null) {
throw new ClassNotFoundException("(classBytes==null)");
} else {
return defineClass(name, classBytes, 0, classBytes.length);
}
}

public void execute(String codeName, byte[] code) {
Class klass = null;
try {
klass = findClass(codeName, code);
TaskIntf task = (TaskIntf) klass.newInstance();
task.execute();
} catch (Exception exception) {
exception.printStackTrace();
}
}
bhjsj 2005-12-21
  • 打赏
  • 举报
回复
自己顶啦.
believefym 2005-12-21
  • 打赏
  • 举报
回复
关注。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧