Applet 中的类加载器:无法访问文件
我有一个应用程序,它使用反射来实例化保存在某个目录中的代码:我创建一个 URLClassLoader,然后使用提供的 URL 加载类;这很好用。我尝试将应用程序移植到Applet。为了加载文本文件和图像,我将代码从使用相对路径更改为使用 getResourceAsStream() ,效果很好。然而,对于类加载器,我仍然有一个 I/O 异常(在我将代码更改为使用流之前,我也用它来获取文本文件和图像):
java.security.AccessControlException: access denied (java.io.FilePermission /.../... read)
要加载的类包含在 jar 文件中(所有其他资源也是如此)。有没有什么方法可以使用诸如 getResourceAsStream() 之类的方法来加载类,并且不会调用安全异常?请注意:我说的是从 java 代码中调用的类加载器,而不是加载 applet 的类加载器。
编辑:有关文件/文件夹结构的更多详细信息:
我的小程序位于包 a, a.MyApplet 中,它使用类 a.aa.Loader ,该类旨在加载存储在另一个文件夹 b 中的类(因此当小程序已加载)。文件夹b包含许多目录b.c_i,其中c_i是唯一的目录。每个目录中都有属于包 xyz 的类,因此 b 的整体文件夹结构为 b.c_i.xyz; z 包含要加载的文件。当我创建 jar 时,顶层如下所示([] 表示文件夹): [a],[b],[data],[images] 其中 [a]={MyApplet,[aa],[aaa]等} 和 [b]={c_1,_c2,...} 其中 c_i={[x]}, [x]={[y]}, [y]={[z]} 最后[z]={类.类}。希望这个符号不会太奇怪。
编辑2:更多细节。
澄清一下:我想要加载的类是其他人的类,全部放在单独的目录“DIR”中(它们不是项目的一部分,也不构成项目本身)。事实上,所有这些类名都是相同的,它们存储在“DIR”内的唯一目录中。我需要一次加载一个文件。换句话说,我想像对待任何其他资源一样对待类文件。
注意:我尝试了一个签名的小程序,它不再抛出安全异常,而是抛出 IO 异常:它无法找到该文件。我检查了目录结构并尝试了多种变体,所有变体都具有相同的结果(它在我的本地计算机上运行)。我不确定这是否真的是一个IO问题或者是否仍然是一个安全问题。
I have an application that uses reflection to instantiate code saved in some directory: I create a URLClassLoader that then loads the classes using the URLs provided; this works fine. I tried to port the application to an Applet. For loading text files and images, I changed the code from using relative paths to use getResourceAsStream()
which works great. For the class loader, however, I still have an I/O exception (which I also used to get with the text files and images before I changed the code to use streams):
java.security.AccessControlException: access denied (java.io.FilePermission /.../... read)
The classes to be loaded are contained in the jar file (as are all the other resources). Is there any way to load classes using something like getResourceAsStream()
that does not invoke a security exception? Please note: I am talking about a class loader invoked from within the java code, not the class loader that loads the applet.
EDIT: More details on the file/folder structure:
My applet is in a package a, a.MyApplet, which makes use of a class a.aa.Loader which is meant to load classes stored in another folder b (hence not loaded when the applet is loaded). The folder b contains many directories, b.c_i, where c_i is a unique directory. In each of these directories are classes that belong to a package x.y.z, so the overall folder structure of b is b.c_i.x.y.z; z contains the file to be loaded. When I create the jar, the top-level looks like follows ([] indicates folder): [a],[b],[data],[images] where [a]={MyApplet,[aa],[aaa],etc.} and [b]={c_1,_c2,...} where c_i={[x]}, [x]={[y]}, [y]={[z]} and finally [z]={Class.class}. Hope the notation is not too weird.
Edit2: More details.
To clarify: the classes I want to load are classes by other people, all placed in a separate directory "DIR" (they are not part of the project nor do they form a project themselves). In fact, all these class names are identical, they are stored in unique directories within "DIR". I need to load one file at a time. In other words, I would like to treat the class files like any other resource.
NOTE: I tried a signed applet and it no longer throws a security exception but an IO one: it can not locate the file. I checked the directory structure and tried numerous variants, all with the same outcome (it works on my local machine). I am not sure if it is really an IO problem or whether it is still a security issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,将项目保留为单独的对象范围是一种不好的语气,所以...
您可以将所有项目打包到一个 jar 文件中。
如果您将课程保留在服务器上只是因为整个
项目太大,您可以使用 pack200 util 压缩它,即
对于 Java Web Start 胖客户端小程序非常有帮助
我建议使用 jnlp 小程序启动类型,因为它提供了
更多选项及其 DeployJava.js
PS
如果你真的坚持使用类加载器加载资源以小程序标准方式执行此操作,我的意思是使用 Anchor 对象,
例如创建类似
SourceBound.java
Test 的 结构.java
好运气
Actually, keeping a project as a separate objects range that's a bad tone so...
You can just pack all your project to a single jar file.
In the case you keep your classes on server just because the whole
project is too big you can compress it with pack200 util which is
very helpful for Java Web Start fat client applets
And I do recommend use jnlp applet launch type because it provides
more options with its DeployJava.js
P.S.
And if you really insist to load resources with class loader do it in a applet standard manner I mean with an Anchor object
for example create structure like
SourceBound.java
Test.java
Good luck