Java JSON库和JDK 1.4中的NoSuchMethodError

Java JSON库和JDK 1.4中的NoSuchMethodError,java,json-lib,Java,Json Lib,我已经下载了最新的JDK1.3兼容二进制文件json-lib-2.4-jdk13.jar,出现以下错误 Exception in thread "main" java.lang.NoSuchMethodError: java.lang.ThreadLocal: method remove()V not found at net.sf.json.AbstractJSON.removeInstance(AbstractJSON.java:221) 我检查了JDK1.4API,发现Threa

我已经下载了最新的JDK1.3兼容二进制文件json-lib-2.4-jdk13.jar,出现以下错误

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.ThreadLocal: method remove()V not found
    at net.sf.json.AbstractJSON.removeInstance(AbstractJSON.java:221)
我检查了JDK1.4API,发现ThreadLocal上的remove方法确实不受支持,并且只在JDK1.5中添加了remove方法

违规代码是:

protected static void removeInstance(Object instance)
{
  Set set = getCycleSet();
  set.remove(instance);
  if (set.size() == 0)
    cycleSet.remove();
}
有人知道我是否错过了一些明显的东西,或者需要额外的下载或其他什么吗。错误实际上是说ThreadLocal#remove()V不存在。那是1.5分。(见?)

以下是json lib 2.4(jdk1.3)中的错误源

AbstractJSON:

   /**
    * 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回答/评论后编辑]:

鉴于lib是开源的,我认为这可能会修复它(未经测试):


我刚刚检查了Thread和ThreadLocal的代码。我认为,如果您至少可以控制用于运行应用程序的命令行,您可以尝试创建线程的特殊版本,它是来自Java1.3的线程与来自Java1.5的线程的合并:添加线程本地支持

然后修复一点ThreadLocal本身:删除泛型和AtomicInteger

现在创建创建这两个类的jar,并在运行应用程序时将它们放入引导类路径


祝你好运。如果幸运的话,这可能会起作用。

Java1.3和1.4已经很老了。你应该考虑升级。是的,现在需要做一点工作,但这比以后遇到一个主要的JDK错误(或操作系统不兼容)要好得多,在这一点上你已经完全失去了工作luck@Adam是的,你当然是对的。我正在处理一些遗留代码,没有必要仅仅更新jdknow@alphazero. 谢谢你的帖子。我的问题是错误地说它从集合中丢失了remove(),而实际上问题是CylceSet扩展的ThreadLocal。我已经更新了我的问题,这是库中的一个bug。看看答案。从其他东西扩展CycleSet不是更简单吗?我们知道他们想要什么——线程范围的对象——地图可以完成这项工作。谢谢你证实我的怀疑。解决方法是恢复到json库的早期版本,因此它是在版本2.4中引入的。对于那些跟随我的人,我用2.3版(json-lib-2.3-jdk13.jar)做了一些事情,这是一个很好的收获。我认为这确实是一个错误。因为您提到的特定版本是专门针对Java版本1.3和1.4运行的。
   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;
      }
   }