title: Error with usin Apache POI
body: <p>I have created a search tool to search word documents in a selected folder for a given keyword and return the filenames that contain the keyword. The error is possibly due to Apache I have used Apache POI in order to read word documents however i get the following error when I compile the project:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
      at sun.misc.Unsafe.ensureClassInitialized(Native Method)
      at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
      at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
      at java.lang.reflect.Field.acquireFieldAccessor(Field.java:949)
      at java.lang.reflect.Field.getFieldAccessor(Field.java:930)
      at java.lang.reflect.Field.get(Field.java:372)
      at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:770)
      at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.&lt;clinit&gt;(Unknown Source)
      at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
      at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:135)
      at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
      at org.apache.poi.xwpf.usermodel.XWPFDocument.&lt;init&gt;(XWPFDocument.java:124)
      at Main.jButton1ActionPerformed(Main.java:176)
      at Main.access$000(Main.java:27)
      at Main$1.actionPerformed(Main.java:78)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.Component.processMouseEvent(Component.java:6504)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
      at java.awt.Component.processEvent(Component.java:6269)
      at java.awt.Container.processEvent(Container.java:2229)
      at java.awt.Component.dispatchEventImpl(Component.java:4860)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4686)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
      at java.awt.Container.dispatchEventImpl(Container.java:2273)
      at java.awt.Window.dispatchEventImpl(Window.java:2713)
      at java.awt.Component.dispatchEvent(Component.java:4686)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
      at java.awt.EventQueue.access$000(EventQueue.java:101)
      at java.awt.EventQueue$3.run(EventQueue.java:666)
      at java.awt.EventQueue$3.run(EventQueue.java:664)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:680)
      at java.awt.EventQueue$4.run(EventQueue.java:678)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  Caused by: java.lang.RuntimeException: Could not instantiate SchemaTypeSystemImpl (java.lang.reflect.InvocationTargetException): is the version of xbean.jar correct?
      at schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.TypeSystemHolder.loadTypeSystem(Unknown Source)
      at schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.TypeSystemHolder.&lt;clinit&gt;(Unknown Source)
      ... 51 more
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
      ... 53 more
  Caused by: org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Incompatible minor version - expecting up to 23, got 24 (schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707.index) - code 3
      at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.&lt;init&gt;(SchemaTypeSystemImpl.java:1522)
      at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:260)
      at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.&lt;init&gt;(SchemaTypeSystemImpl.java:183)
      ... 57 more
  </code></pre>
  
  <p>The complete code is as follows:</p>
  
  <pre><code>import java.io.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.SwingUtilities;
  import javax.swing.filechooser.*;
  import java.util.Scanner;
  import javax.swing.JOptionPane;
  import java.util.ArrayList;
  import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  import org.apache.poi.xwpf.usermodel.XWPFDocument;
  
  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  
  /*
   * Main.java
   *
   * Created on Jul 4, 2014, 8:19:21 AM
   */
  /**
   *
   * @author Yağız
   */
  public class Main extends javax.swing.JFrame {
  
      Boolean check = true;
      File[] filelist;
      /** Creates new form Main */
      public Main() {
          initComponents();
      }
  
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;
      private void initComponents() {
          bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
  
          jFrame1 = new javax.swing.JFrame();
          jMenuBar1 = new javax.swing.JMenuBar();
          jMenu1 = new javax.swing.JMenu();
          jMenu2 = new javax.swing.JMenu();
          label1 = new java.awt.Label();
          jTextField1 = new javax.swing.JTextField();
          jButton1 = new javax.swing.JButton();
          jButton2 = new javax.swing.JButton();
  
          javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
          jFrame1.getContentPane().setLayout(jFrame1Layout);
          jFrame1Layout.setHorizontalGroup(
              jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 400, Short.MAX_VALUE)
          );
          jFrame1Layout.setVerticalGroup(
              jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 300, Short.MAX_VALUE)
          );
  
          jMenu1.setText("File");
          jMenuBar1.add(jMenu1);
  
          jMenu2.setText("Edit");
          jMenuBar1.add(jMenu2);
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jFrame1, org.jdesktop.beansbinding.ELProperty.create("CV Search"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
          bindingGroup.addBinding(binding);
  
          label1.setText("Keyword:");
  
          jButton1.setText("Find");
          jButton1.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton1ActionPerformed(evt);
              }
          });
  
          jButton2.setText("Select Folder");
          jButton2.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton2ActionPerformed(evt);
              }
          });
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addGap(29, 29, 29)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(layout.createSequentialGroup()
                          .addComponent(jButton2)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                          .addComponent(jButton1))
                      .addGroup(layout.createSequentialGroup()
                          .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                  .addGap(29, 29, 29))
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addGap(22, 22, 22)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                      .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(jButton2)
                      .addComponent(jButton1))
                  .addGap(37, 37, 37))
          );
  
          bindingGroup.bind();
  
          java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
          setBounds((screenSize.width-278)/2, (screenSize.height-158)/2, 278, 158);
      }// &lt;/editor-fold&gt;
  
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          //Select Folder Button
          try{
              JFileChooser fc = new JFileChooser();
              fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
              fc.showOpenDialog(null);
              if(fc.getSelectedFile()==null){
                  check = false;
              } 
              else 
              {
                  File folder = new File(fc.getSelectedFile().getPath());
                  filelist = folder.listFiles();
                  if( filelist.length == 0 ){
                  // dialog: folder is empty
                      check = false;
                  }
                  else
                  {
                      check = true;
                  }
               }
          }catch(Exception e){
              e.printStackTrace();
              JOptionPane.showMessageDialog(null, "Error");
          }
      }                                        
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          //Find button for finding the word in word documents in the selected folder   
          String keyword = jTextField1.getText();
          int count=0;
  
          while (! check ) {
              JOptionPane.showMessageDialog(null, "Select a Folder!");
              return;
          }
  
          if(keyword.equals("")){
              JOptionPane.showMessageDialog(null, "No keyword found!");
              return;
          }
  
          ArrayList result = new ArrayList();
  
          try
          {
              for( int i=0; i &lt; filelist.length; i++){
                  // maybe also .doc?
                  if( !filelist[i].getName().endsWith( ".docx" ) ) continue;
                  count++;
                  //...
                  XWPFDocument input = new XWPFDocument(new FileInputStream(filelist[i]));
                  XWPFWordExtractor extract = new XWPFWordExtractor(input);
                  Scanner scan = new Scanner(extract.getText());
                  while(scan.hasNext())
                  {
                      String word = scan.next();
                      if( keyword.equals(word) ) {
                          result.add(filelist[i].getName());
                      break;
                      }
                  }
              }
  
              if( count == 0 ){
                  JOptionPane.showMessageDialog(null,"Please select a valid document");
                  return;
              }
  
              //...
              if(result.isEmpty()){
                  JOptionPane.showMessageDialog(null,"No match found!");
                  return;
              }
              else {
                  StringBuilder sb = new StringBuilder();
                  for( int j = 0; j &lt; result.size(); j++){
                      sb.append( result.get(j) ).append( "\n" );
                  }
                  JOptionPane.showMessageDialog( null, sb.toString() );
              }              
          } catch (Exception e){
             e.printStackTrace();
             JOptionPane.showMessageDialog(null, "Error!");
          }
      }                                        
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
  
              public void run() {
                  new Main().setVisible(true);
              }
          });
      }
      // Variables declaration - do not modify
      private javax.swing.JButton jButton1;
      private javax.swing.JButton jButton2;
      private javax.swing.JFrame jFrame1;
      private javax.swing.JMenu jMenu1;
      private javax.swing.JMenu jMenu2;
      private javax.swing.JMenuBar jMenuBar1;
      private javax.swing.JTextField jTextField1;
      private java.awt.Label label1;
      private org.jdesktop.beansbinding.BindingGroup bindingGroup;
      // End of variables declaration
  }
  </code></pre>
  
  <p>Finally here are the .jar files I have added to project:</p>
  
  <pre><code>poi-3.10-FINAL-20140208.jar
  poi-examples-3.10-FINAL-20140208.jar
  poi-excelant-3.10-FINAL-20140208.jar
  poi-ooxml-3.10-FINAL-20140208.jar
  poi-ooxml-schemas-3.10-FINAL-20140208.jar
  poi-scratchpad-3.10-FINAL-20140208.jar
  xmlbenas-2.2.0.jar
  dom4j-1.6.1.jar
  ooxml-schemas-1.0.jar
  </code></pre>
  
  <p>Why am I getting this error?</p>
  
  <p>edit: didn't have xbeans 2.20 in my project which has the same version with xmlbeans but still get the error when i added xbean.jar</p>