Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: org/apache/lucene/codecs/Codec

I have a project needing Lucene(4.3.0) and add the following dependenies: lucene-core,lucene-analyzers-common,lucene-queries,lucene-queryparser.

And,after add lucene-codecs dependency,also get the same error.

but, the lucene-core jar contains the Codec class

----- begin exception -----

I/TestRunner(2443): java.lang.NoClassDefFoundError: org/apache/lucene/codecs/Codec
I/TestRunner(2443):     at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:118)
I/TestRunner(2443):     at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:144)
I/TestRunner(2443):     at com.my.search.SearchIndexManager.newWriter(SearchIndexManager.java:301)
I/TestRunner(2443):     at com.my.search.SearchIndexManager.addIndexState(SearchIndexManager.java:95)
I/TestRunner(2443):     at com.my.SearchOperation.addIndexer(SearchOperation.java:68)
I/TestRunner(2443):     at com.my.test.SearchOperationTest.testSearchWithFilter(SearchOperationTest.java:208)
I/TestRunner(2443):     at java.lang.reflect.Method.invokeNative(Native Method)
I/TestRunner(2443):     at java.lang.reflect.Method.invoke(Method.java:511)
I/TestRunner(2443):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
I/TestRunner(2443):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
I/TestRunner(2443):     at junit.framework.TestCase.runBare(TestCase.java:134)
I/TestRunner(2443):     at junit.framework.TestResult$1.protect(TestResult.java:115)
I/TestRunner(2443):     at junit.framework.TestResult.runProtected(TestResult.java:133)
I/TestRunner(2443):     at junit.framework.TestResult.run(TestResult.java:118)
I/TestRunner(2443):     at junit.framework.TestCase.run(TestCase.java:124)
I/TestRunner(2443):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
I/TestRunner(2443):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
I/TestRunner(2443):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
I/TestRunner(2443):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
I/TestRunner(2736): Caused by: java.lang.IllegalArgumentException: A SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene42' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath.The current classpath supports the following names: []
I/TestRunner(2736):     at org.apache.lucene.util.NamedSPILoader.lookup(NamedSPILoader.java:109)
I/TestRunner(2736):     at org.apache.lucene.codecs.Codec.forName(Codec.java:95)
I/TestRunner(2736):     at org.apache.lucene.codecs.Codec.<clinit>(Codec.java:122)
I/TestRunner(2736):     ... 19 more

----- end exception -----

like image 900
CHmoonKa Avatar asked Feb 15 '23 20:02

CHmoonKa


2 Answers

Googling turns up some interesting results.

https://issues.apache.org/jira/browse/LUCENE-4204

According to the issue above, the Android APK builder removes some files in the META-INF/services directory, which is required by Lucene. They suggest some workarounds, like adding the necessary files from the lucene jars directly to the APK via some ant tasks done after the APK builder is done. Just be careful, since some lucene jars use the same files in the META-INF/services directory, and you may overwrite them uneccessarily.

I would actually suggest that you use the android maven plugin, as they have applied a fix for this already (see https://code.google.com/p/maven-android-plugin/issues/detail?id=97). Just use the latest version of the plugin (or any version above 3.2.1).

like image 151
Russell Santos Avatar answered Feb 18 '23 11:02

Russell Santos


It seems that your problem is not that Codec class is not found but that another class (which is an SPI provider) is not found (see Caused by). If you see in Lucene package description at the very bottom (also see here) this class is declared as an SPI provider in the META-INF folder of one of your jars.

So search your jars to find a META-INF/services/org.apache.lucene.codecs.Codec file, open it and see the name of the class. You are missing the jar that contains that class

Hope it helps

like image 37
c.s. Avatar answered Feb 18 '23 11:02

c.s.