Applet 中的类加载器:无法访问文件 [英] Classloader in Applet: Can't access files

查看:22
本文介绍了Applet 中的类加载器:无法访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用反射来实例化保存在某个目录中的代码:我创建了一个 URLClassLoader,然后使用提供的 URL 加载类;这工作正常.我试图将应用程序移植到 Applet.为了加载文本文件和图像,我将代码从使用相对路径改为使用 getResourceAsStream() ,效果很好.然而,对于类加载器,我仍然有一个 I/O 异常(在我将代码更改为使用流之前,我也曾用它来获取文本文件和图像):

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)

要加载的类包含在 jar 文件中(与所有其他资源一样).有没有办法使用诸如 getResourceAsStream() 之类的东西来加载类,它不会调用安全异常?请注意:我说的是从 Java 代码中调用的类加载器,而不是加载小程序的类加载器.

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.

有关文件/文件夹结构的更多详细信息:

More details on the file/folder structure:

我的小程序在包 a 中,a.MyApplet,它使用类 a.aa.Loader,该类旨在加载存储在另一个文件夹 b 中的类(因此在加载小程序时不会加载).文件夹 b 包含许多目录 b.c_i,其中 c_i 是一个唯一目录.在这些目录中的每一个中都有属于一个包 x.y.z 的类,所以 b 的整体文件夹结构是 b.c_i.x.y.z;z 包含要加载的文件.当我创建 jar 时,顶层如下所示([] 表示文件夹): [a],[b],[data],[images] where [a]={MyApplet,[aa],[aaa],etc.} 和 [b]={c_1,_c2,...} 其中 c_i={[x]}, [x]={[y]}, [y]={[z]} 和最后 [z]={Class.class}.希望符号不要太奇怪.

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.

澄清:我想加载的类是其他人的类,都放在一个单独的目录DIR"中(它们不是项目的一部分,也不构成项目本身).事实上,所有这些类名都是相同的,它们存储在DIR"内的唯一目录中.我需要一次加载一个文件.换句话说,我想像对待任何其他资源一样对待类文件.

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.

注意:我尝试了一个签名的小程序,它不再抛出安全异常而是一个 IO 异常:它无法定位文件.我检查了目录结构并尝试了许多变体,结果都相同(它在我的本地机器上工作).不知道是真的IO问题还是安全问题.

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.

推荐答案

实际上,将项目作为一个单独的对象范围是不好的,所以...

Actually, keeping a project as a separate objects range that's a bad tone so...

  • 您可以将所有项目打包到一个 jar 文件中.

  • You can just pack all your project to a single jar file.

如果您将课程保留在服务器上只是因为整个项目太大你可以用pack200 util压缩它对 Java Web Start 胖客户端小程序非常有帮助

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

我确实推荐使用 jnlp 小程序启动类型,因为它提供更多选项及其 DeployJava.js

And I do recommend use jnlp applet launch type because it provides more options with its DeployJava.js

附言

如果您真的坚持使用类加载器加载资源,请以小程序标准方式加载资源,我的意思是使用 Anchor 对象

And if you really insist to load resources with class loader do it in a applet standard manner I mean with an Anchor object

例如创建像

  • |- 包装图片-
  • |imageA.png
  • |imageB.png
  • |锚.class
  • |SourceBound.class

SourceBound.java

SourceBound.java

public class SourceBound
{

  /**
    Conception only...
  **/
  public SourceBound(){}

  public ImageIcon getImageA()
  {
    ImageIcon icon;
    Image image;

    image=ImageIO.read(Anchor.class.getResourceAsStream("imageA.png"));
    icon=new ImageIcon(image);

    return icon;
  }
}

<小时>

  • |包装测试
  • |测试类
  • 测试.java

    public class Test
    {
    
    SourceBound sourceBound=new SourceBound();
    
    Test()
    {
      JButton button=new JButton();
      button.setIcon(sourceBound.getImageA());
    
    }
    
    }
    

    祝你好运

    这篇关于Applet 中的类加载器:无法访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆