1

我正在用 Django 开发一个 Web 服务器,并在一个页面中嵌入了一个 Java Applet。

我必须在实例化时向这个小程序传递一个 JSon 数组。完全没有问题,我已经在 Eclipse 中对其进行了测试,没有任何错误或问题。

当我把它放在网页上时,大问题就出现了。

首先我尝试使用Jackson,这是页面上的代码:

  <applet name="mapGenerator"
            id="mapGenerator"  
            code="main.MapGenerator.class"
            archive="{{STATIC_URL}}jar/MapGenerator.jar, 
                            {{STATIC_URL}}jar/plugin.jar
                            {{STATIC_URL}}jar/jackson-annotations-2.1.1.jar,
                            {{STATIC_URL}}jar/jackson-core-2.1.1.jar,
                            {{STATIC_URL}}jar/jackson-databind-2.1.1.jar"
                     >
                    <param name=id value="{{building.id}}">
                    <param name=piani value='{{floors}}'>  
            MAYSCRIPT
    </applet>

解析Json的Java代码:

  public static  Floor[] parse(String input, URL codebase) {

        ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
    BaseFloor[] bfloors=null;

        try {
              bfloors = mapper.readValue(input, BaseFloor[].class);
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
          for (int i=0; i < bfloors.length; i++) {

        Floor f = new Floor(bfloors[i]);
        f.setCodeBase(codebase);
        floors[i] = f;
    }
    return floors;
}

这是我尝试在网页中使用 Java 控制台时遇到的错误:

  Exception in thread "thread applet-main.MapGenerator.class-1" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect
at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>(VisibilityChecker.java:172)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:195)
at objects.Floor.parse(Floor.java:37)
at main.MapGenerator.loadParameters(MapGenerator.java:67)
at main.MapGenerator.start(MapGenerator.java:31)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1698)
at java.lang.Thread.run(Thread.java:662)
  Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonAutoDetect
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:250)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:180)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:161)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 7 more

所以我遇到了一些奇怪的实现问题,我尝试使用google-gson

和以前一样,这里是网页代码:

   <applet name="mapGenerator"
            id="mapGenerator"  
            code="main.MapGenerator.class"
            archive="{{STATIC_URL}}jar/MapGenerator.jar, 
                            {{STATIC_URL}}jar/gson-2.2.2.jar,
                            {{STATIC_URL}}jar/plugin.jar
                     >
                    <param name=id value="{{building.id}}">
                    <param name=piani value='{{floors}}'>  
            MAYSCRIPT
    </applet>

和Java代码:

  public static  Floor[] parse(String input, URL codebase) {

    Gson gson = new Gson();

    BaseFloor[] bfloors = gson.fromJson(input, BaseFloor[].class);

    Floor[] floors = new Floor[bfloors.length];

    for (int i=0; i < bfloors.length; i++) {

        Floor f = new Floor(bfloors[i]);
        f.setCodeBase(codebase);
        floors[i] = f;
    }
    return floors;
}

我在 Eclipse 上运行小程序时没有收到任何错误,但是当我在网页上进行测试时……我收到另一个错误:

  java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:107)
at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:84)
at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:66)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:71)
at com.google.gson.Gson.getAdapter(Gson.java:353)
at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
at com.google.gson.Gson.getAdapter(Gson.java:353)
at com.google.gson.Gson.fromJson(Gson.java:794)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at objects.Floor.parse(Floor.java:33)
at main.MapGenerator.loadParameters(MapGenerator.java:67)
at main.MapGenerator.start(MapGenerator.java:31)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1698)
at java.lang.Thread.run(Thread.java:662)
  Eccezione: java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)

这是我试图解析的 BaseFloor 类,它位于静态函数的同一个包中

  package objects;


  public class BaseFloor {

      public int numero_di_piano;
      public String link;
      public String bearing;

      public float getBearing() {
    return Float.parseFloat(bearing);
      }

      public int getFloor() {
    return numero_di_piano;
      }

      public String getLink() {
    return link;
      }

      public String toString() {
    return numero_di_piano + " " + bearing + " " + link;
      }
  }

这是我传递给小程序的 JSON:

  [{"bearing": "354.00", "link": "/media/floors/Foto_del_63333991-02-2455618_alle_17_35_6.jpg", "numero_di_piano": 0}, {"bearing": "354.00", "link": "/media/floors/IMG_20111009_171138_7.jpg", "numero_di_piano": 1}]

我很好奇:这是我实现代码的问题,还是安全问题,因为我的小程序没有签名,所以无法使用反射?

如果您需要什么,请随意询问。

先感谢您。

编辑

我已经尝试了你建议的@Nikita,并且错误改变了!

  java.security.AccessControlException: access denied (java.lang.RuntimePermission accessDeclaredMembers)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkMemberAccess(SecurityManager.java:1662)
at java.lang.Class.checkMemberAccess(Class.java:2157)
at java.lang.Class.getDeclaredMethods(Class.java:1790)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass._addMemberMethods(AnnotatedClass.java:593)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass.resolveMemberMethods(AnnotatedClass.java:421)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass.memberMethods(AnnotatedClass.java:243)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addMethods(POJOPropertiesCollector.java:442)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collect(POJOPropertiesCollector.java:232)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:142)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forDeserialization(BasicClassIntrospector.java:81)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forDeserialization(BasicClassIntrospector.java:11)
at com.fasterxml.jackson.databind.DeserializationConfig.introspect(DeserializationConfig.java:507)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:326)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:2898)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2792)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
at objects.Floor.parse(Floor.java:40)
at main.MapGenerator.loadParameters(MapGenerator.java:67)
at main.MapGenerator.start(MapGenerator.java:31)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1698)
at java.lang.Thread.run(Thread.java:662)
  Eccezione: java.security.AccessControlException: access denied (java.lang.RuntimePermission accessDeclaredMembers)
4

1 回答 1

2

尝试{{STATIC_URL}}jar/plugin.jar在页面中插入逗号。可能是 java 插件因为这个而忽略了 jackson-annotations.jar 并且你得到NoClassDefFoundError.
这是一个类似的问题:NoClassDefFoundError JsonAutoDetect while parsing JSON object and missingjackson-annotations导致问题。

于 2012-11-24T23:08:22.310 回答