HDFS + WEB 项目 报java.lang.VerifyError... 异常

今天在用web项目整合 HDFS 的时候发生了非常诡异的事情。我将java项目的HDFS功能整合至 web项目的时候创建FileSystem对象就报Exception in thread "main" java.lang.VerifyError: (class: com/google/common/collect/Interners, method: newWeakInterner signature: ()Lcom/google/common/collect/Interner;) Incompatible argument to function

异常。而同样的程序在java项目中却运行的正常。

FileSystem创建代码

File workaround = new File(".");
System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath());
new File("./bin").mkdirs();
new File("./bin/winutils.exe").createNewFile();
String hdfsURL = "hdfs://192.168.1.118:50040";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI(hdfsURL), configuration);
抛异常:

Exception in thread "main" java.lang.VerifyError: (class: com/google/common/collect/Interners, method: newWeakInterner signature: ()Lcom/google/common/collect/Interner;) Incompatible argument to function
    at org.apache.hadoop.util.StringInterner.<clinit>(StringInterner.java:48)
    at org.apache.hadoop.conf.Configuration.loadResource(Configuration.java:2472)
    at org.apache.hadoop.conf.Configuration.loadResources(Configuration.java:2364)
    at org.apache.hadoop.conf.Configuration.getProps(Configuration.java:2281)
    at org.apache.hadoop.conf.Configuration.get(Configuration.java:888)
    at org.apache.hadoop.conf.Configuration.getTrimmed(Configuration.java:907)
    at org.apache.hadoop.conf.Configuration.getBoolean(Configuration.java:1308)
    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:366)


因此我怀疑,这里存在web项目的包和hdfs相关包冲突了。于是花了好长的时间找啊找,终于找到了冲突的包。

冲突来自 Java_EE_6 中的weld-osgi-bundle.jar 和 guava-11.0.2.jar 这两个包。 com/google/common/collect/Interners 类是 HDFS在创建FileSystem时所需要使用的一个创建Interner类的工具类,但是 weld-osgi-bundle.jar和guava-11.0.2.jar 存在了一样的结构,而weld-osgi-bundle.jar中却不存在Interners类,导致在运行过程中的冲突。

解决方法一
将web工程应用Java EE 5。笔者的项目使用Java EE 5已经足够了,如果你们的项目中有使用到Java EE 6的功能可以通过另外导入需要的工具实现。

HDFS + WEB 项目 报java.lang.VerifyError... 异常_第1张图片

解决方法二:

将Java EE 6 的包通过 ADD JARs... 的方式导入。
HDFS + WEB 项目 报java.lang.VerifyError... 异常_第2张图片

注意:不要将 Java EE 6中的包直接放到WebRoot/WEB-INF/lib 下,这样同样会导致包冲突

你可能感兴趣的:(HDFS + WEB 项目 报java.lang.VerifyError... 异常)