java - JSON-Lib 和 JDK 1.4 的 NoSuchMethodError

标签 java json-lib

我已经下载了名为 json-lib-2.4-jdk13.jar 的据称最新的 JDK 1.3 兼容二进制文件,但出现以下错误。

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.ThreadLocal: method remove()V not found
    at net.sf.json.AbstractJSON.removeInstance(AbstractJSON.java:221)

我检查了 JDK 1.4 API 并注意到 ThreadLocal 上的 remove 方法确实不受支持,并且仅在 JDK 1.5 中添加

违规代码是:

protected static void removeInstance(Object instance)
{
  Set set = getCycleSet();
  set.remove(instance);
  if (set.size() == 0)
    cycleSet.remove();
}

有谁知道我是否遗漏了一些明显的东西,或者需要额外下载什么?

最佳答案

Set#remove(Object) 肯定是defined in Java 1.3 .该错误实际上是说 ThreadLocal#remove()V 不存在。那是在 1.5 中出现的。 (看到了吗?No such method!)

这里是json-lib 2.4 (jdk1.3) bug的来源

抽象JSON:

   /**
    * Removes a reference for cycle detection check.
    */
   protected static void removeInstance( Object instance ) {
      Set set = getCycleSet();
      set.remove( instance );
      if(set.size() == 0) {
          cycleSet.remove();   // BUG @ "line 221"
      }
   }

因为在 CycleSet.java 中我们看到:

   private static class CycleSet extends ThreadLocal {
      protected Object initialValue() {
         return new SoftReference(new HashSet());
      }

      public Set getSet() {
         Set set = (Set) ((SoftReference)get()).get();
         if( set == null ) {
             set = new HashSet();
             set(new SoftReference(set));
         }
         return set;
      }
   }

但是ThreadLocal(1.3)没有这样的方法。

[在@AlexR 回答/评论后编辑]:

鉴于该库是开源的,我认为这可能会解决它(未测试):

   private static class CycleSet extends ThreadLocal {
      protected Object initialValue() {
         return new SoftReference(new HashSet());
      }
      /** added to support JRE 1.3 */
      public void remove() {
          this.set(null);
      }
      public Set getSet() {
         Set set = (Set) ((SoftReference)get()).get();
         if( set == null ) {
             set = new HashSet();
             set(new SoftReference(set));
         }
         return set;
      }
   }

关于java - JSON-Lib 和 JDK 1.4 的 NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6928584/

相关文章:

java - Maven Jetty 插件 stopPort 和 stopKey 丢失或无效

java - kafka- Producer-perf-test.sh 有多少个生产者?

java - java有 "LinkedConcurrentHashMap"数据结构吗?

java - Xml 到 JSON 单值数组处理

java - 如何使用 json-lib 制作类似的 json 格式?

Java Json 解析器

java - 如何更改 Android 按钮颜色 onClick?

java - 数据库分布

java - 使用 Gson 创建特定的 JSON 字符串