我已经下载了名为 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/