---
title: "导致Lucene无法索引查询的NoClassDefFoundError"
author: "huqi"
date: "2008-03-07T15:52:00Z"
tags: ["Apache", "Lucene", "应用服务器", "搜索引擎"]
slug: "lucene-noclassdeffounderror"
draft: false
lastmod: "2008-03-07T15:52:00Z"
id: 169469
url: "https://huqi.iteye.com/blog/169469"
---

Lucene乃是当今免费开源搜索引擎的霸主，确实它十分好用，发展势头也很生猛，在Apache组织的支持下不断的更新、推出新版本。

但是其存在一个隐藏的很深的bug，相信困扰了不少和我一样研究使用过它的人，这个bug从早期的版本到目前的V2.3.1一个存在，不能不说是一个遗憾。

具体触发这个bug的原因很复杂，在某些环境、服务器、应用中……

表现为建立索引文件和执行查询时报“NoClassDefFoundError”，跟进代码会发现是“SegmentReader”这个类型不能创建实例，但怎么看这个类都是存在的。其实问题在这（SegmentReader.java）：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)  /\** \*//\** The class which implements SegmentReader. \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  private static Class IMPL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)  static ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      String name =  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.getProperty("org.apache.lucene.SegmentReader.class",  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                           SegmentReader.class.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      IMPL = Class.forName(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    } catch (ClassNotFoundException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      throw new RuntimeException("cannot load SegmentReader class: " + e, e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    } catch (SecurityException se) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        IMPL = Class.forName(SegmentReader.class.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      } catch (ClassNotFoundException e) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        throw new RuntimeException("cannot load default SegmentReader class: " + e, e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)  }

看到这句没有“System.getProperty("org.apache.lucene.SegmentReader.class", SegmentReader.class.getName());”，鬼知道它什么时候把这个系统属性设入内存中的！它的本意是想动态加载SegmentReader，但反而有可能弄巧成拙，导致Class.forName报出NoClassDefFoundError，更要命的是在static{}代码段中的Error不会明确的显示在异常堆栈中，任你用什么debug手段都不能立即定位问题。

知道了问题，最简单的解决方法，很直接，改代码。把那个IMPL和static{}段干脆去掉，不利用系统属性动态加载就是了，就直接使用SegmentReader，就认准它了:-)