1

I have written a Java Program using Lucene in Eclipse Juno. Whenever I try to run it, it is giving the following errors:

Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class

at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at parser.BuildMainIndex.setUp(BuildMainIndex.java:339)
at parser.luceneDemo.main(luceneDemo.java:10)

and source of the error i.e. the line in BuildManinIndex.java is:

        doc.add(new IntField("startTime1",startTime1,Field.Store.YES));

Here startTime1 is a field in document to be indexed. I was earlier using Lucene 3.6.0 and now I am using Lucene 4.3.0. I have not imported any thing from java.net.URL. I have no clue of the possible cause of this error. Please help.

EDIT: This following short program I have written.

writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_CURRENT),IndexWriter.MaxFieldLength.UNLIMITED);

Document doc=new Document();
doc.add(new Field("title","XYZ",Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("address","ABC Road",Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("city","Mumbai",Field.Store.YES,Field.Index.ANALYZED));
doc.add(new IntField("startTime1",900,Field.Store.YES));
doc.add(new IntField("finishTime1",1000,Field.Store.YES));
doc.add(new IntField("startTime2",9999,Field.Store.YES));
doc.add(new IntField("finishTime2",9999,Field.Store.YES));

writer.addDocument(doc);
writer.close();
Joy
  • 4,197
  • 14
  • 61
  • 131
  • That doesn't look like it would cause that problem. Can you reproduce this with a short but *complete* program? Have you rebuilt everything from scratch against Lucence 4.3.0? – Jon Skeet Jun 07 '13 at 05:49
  • Okay Sir I am trying it. – Joy Jun 07 '13 at 05:57
  • Yes Sir. Error is still coming. Now i Have written a short program for just adding one document in the index. Whenever I comment this line error disappears. – Joy Jun 07 '13 at 06:02
  • 1
    Please post that short but complete program, so that we can reproduce it for ourselves. – Jon Skeet Jun 07 '13 at 06:03
  • @JonSkeet Sir, have you got any clue to solve the problem? – Joy Jun 07 '13 at 07:42
  • 1
    No, please post a short but *complete* program. One that anyone can copy, paste into a new file, compile against Lucene 4.3.0, and run. – Jon Skeet Jun 07 '13 at 08:03

1 Answers1

1

The error message is a bit confusing because you are not inheriting the IntField class, you are creating an instance of it, right?

doc.add(new IntField("startTime1", startTime1, Field.Store.YES));

The problem is in the first part of that statement doc.add(..). There was a change to the Document class between Lucene versions 3.6.0 and 4.x - add(..) in 3.6 is accepting Fieldable (class API), and in 4.1.0 is accepting IndexableField (class API).

Useful article on the VerifyError. Or this StackOverflow answer.

Also, there is no IntField in Lucene 3.6.0, i.e. the class is from Apache Solr (class API) but there is one in Lucene 4.1.0. You are probably using the 3.6 version of IntField which is not the same as Lucene's - please check your import statements and your classpath.

UPDATE

Joy, the easiest solution for you is to remove the old Lucene JAR files from /home/abhishek/mtp/stage-2/software/apache-tomcat-7.0.37/lib and put the new ones, and Refresh the project in Eclipse.

My advice for you is to switch to Apache Maven for dependency management (there is a Maven plug-in for Eclipse). In Eclipse you create a "New Maven Project" and add the dependencies to pom.xml file - an example for Lucene. This way you don't have to copy any JAR files into your project or Tomcat.

Good luck.

Community
  • 1
  • 1
Cebence
  • 2,406
  • 2
  • 19
  • 20
  • I have imported Lucene 4.2.0 but as I am writing it as a part of Dynamic Web Project Lucene 3.6.0.jar is inlcuded in Apache Tomcat 7.0. – Joy Jun 07 '13 at 06:16
  • 1
    Check your `import` statements to see which `IntField` you are importing, the Solr (Lucene 3.6) one or Lucene 4.0. Using two different versions of a JAR is **not a good idea** as you will get strange classpath issues. – Cebence Jun 07 '13 at 06:21
  • Yes Sir I have imported Lucene 4.2.0. But as I am writing it as a part of Dynamic Web Project so in Tomcat7.0 libraries a Lucene 3.6.0 is included. Is it the cause of the error? – Joy Jun 07 '13 at 06:27
  • You import statement should read `import org.apache.lucene.document.IntField;`. That should make the error go away. But two different Lucene versions are certainly the cause. You should try replacing embedded 3.6 version with the 4.1. – Cebence Jun 07 '13 at 06:32
  • Yes Sir I have imported the statement. But actually I am new to developing web projects that is why I do not have much idea to do that. Could you please help me to replace the old jar file included in Tomcat library? – Joy Jun 07 '13 at 06:36
  • You should check the `${tomcat_folder}/lib/`, and remove the `*lucene-3.6*.jar` file (the file names are not exact). You could put the new `lucene-4.3.0.jar` there, but you shouldn't do that - leave the Lucene 4.3 JAR in the WebApp's `lib` folder. – Cebence Jun 07 '13 at 07:28
  • You mean, if I put the Lucene new jar file in WebApps/lib folder the problem may be fixed, right? – Joy Jun 07 '13 at 07:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31379/discussion-between-cebence-and-joy) – Cebence Jun 07 '13 at 07:41