在类加载器小程序:无法访问文件 [英] Classloader in Applet: Can't access files

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

问题描述

我有一个使用反射来实例化code保存在某个目录中的应用程序:我创建了一个URLClassLoader的那然后加载使用提供的URL类别;这工作正常。我试图端口的应用程序到一个Applet。对于加载文本文件和图片,我使用相对路径使用改变了code 的getResourceAsStream()伟大的工程。对于类加载器,但是,我仍然有一个I / O异常(这也是我用来获取与文本文件和图片前,我改变了code使用流):

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的code,而不是把这个小程序的类加载器内调用的类加载器

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.MyApplet,这使得使用一类a.aa.Loader其目的是要加载存储在另一个文件夹B类(因此不能当小程序被加载加载)。该文件夹B包含多个目录,b.c_i,其中C_I是一个独特的目录。在每个目录中是属于一个包X.Y.Z,所以b的整体夹结构是b.c_i.x.y.z班; Z含有要加载的文件。当我创建罐子,顶层的样子如下([]表示文件夹):[A],[B],[数据],[图片],其中[A] = {MyApplet,[AA],[AAA]等}和[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.

在你把你的类服务器只是因为整个案件
工程量太大,你可以COM preSS它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 applet的启动类型,因为它提供了
更多的选择与 DeployJava.js

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

例如创建结构像


  • | - 包图片 -

  • | imageA.png

  • | imageB.png

  • | Anchor.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;
  }
}



  • |封装测试

  • |的Test.class

  • Test.java

    Test.java

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

    好运气

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

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