我已经下载了名为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)
我检查了JDK1.4API,注意到ThreadLocal上的remove方法确实不受支持,并且只在JDK1.5中添加
有问题的代码是:
protected static void removeInstance(Object instance)
{
Set set = getCycleSet();
set.remove(instance);
if (set.size() == 0)
cycleSet.remove();
}
有没有人知道我是否遗漏了一些明显的东西,或者需要额外的下载?
发布于 2011-08-03 23:46:00
Set#remove(Object)当然是defined in Java 1.3。错误实际上是说ThreadLocal#remove()V不存在。这是1.5年来的。(参见?No such method!)
以下是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是开源的,我认为这可能会修复它(未经过测试):
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;
}
}
发布于 2011-08-03 23:48:07
我刚刚检查了Thread和ThreadLocal的代码。我认为,如果您至少可以控制用于运行应用程序的命令行,您可以尝试创建特殊版本的Thread,它是java 1.3中的Thread和java 1.5中的Thread的合并:添加线程本地支持。
然后修改一下ThreadLocal本身:删除泛型和曾经使用过的AtomicInteger。
现在创建创建这两个类的jar,并在运行应用程序时将它们放到引导类路径中。
祝你好运。如果你幸运的话,这可能会起作用。
https://stackoverflow.com/questions/6928584
复制相似问题