title: NoClassDefFoundError: org/apache/poi/OldFileFormatException when editing a .doc file (WORD) with java?
body: <p>I try to edit a .doc file (<em>WORD</em>) with java. I used the Apache POI library, but the results are negatives. I have a previously document (a template), and i want to replace a specific word. (e.g. that i can replace "name" with a String whatever i want). </p>
  
  <p>Here a example of the internet that i try to use: </p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Set;
  import org.apache.poi.hwpf.HWPFDocument;
  import org.apache.poi.hwpf.usermodel.CharacterRun;
  import org.apache.poi.hwpf.usermodel.Paragraph;
  import org.apache.poi.hwpf.usermodel.Range;
  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  
  public class Prueba {
       public void searchAndReplace(String inputFilename,
                                   String outputFilename,
                                   HashMap&lt;String, String&gt; replacements) {        
          File inputFile = null;
          File outputFile = null;
          FileInputStream fileIStream = null;
          FileOutputStream fileOStream = null;
          BufferedInputStream bufIStream = null;
          BufferedOutputStream bufOStream = null;
          POIFSFileSystem fileSystem = null;
          HWPFDocument document = null;
          Range docRange = null;
          Paragraph paragraph = null;
          CharacterRun charRun = null;
          Set&lt;String&gt; keySet = null;
          Iterator&lt;String&gt; keySetIterator = null;
          int numParagraphs = 0;
          int numCharRuns = 0;
          String text = null;
          String key = null;
          String value = null;
  
          try {
              inputFile = new File(inputFilename);
              fileIStream = new FileInputStream(inputFile);
              bufIStream = new BufferedInputStream(fileIStream);
              fileSystem = new POIFSFileSystem(bufIStream);
              document = new HWPFDocument(fileSystem);
  
              docRange = document.getRange();
              numParagraphs = docRange.numParagraphs();
  
              keySet = replacements.keySet();
  
              for(int i = 0; i &lt; numParagraphs; i++) {
                  paragraph = docRange.getParagraph(i);
                  text = paragraph.text();
  
                  numCharRuns = paragraph.numCharacterRuns();
                  for(int j = 0; j &lt; numCharRuns; j++) {
                      charRun = paragraph.getCharacterRun(j);
  
                      text = charRun.text();
                      keySetIterator = keySet.iterator();
                      while(keySetIterator.hasNext()) {
  
                          key = keySetIterator.next();
                          if(text.contains(key)) {
  
                              value = replacements.get(key);
                              int start = text.indexOf(key);
                              charRun.replaceText(key, value, start);
  
  
                              docRange = document.getRange();
                              paragraph = docRange.getParagraph(i);
                              charRun = paragraph.getCharacterRun(j);
                              text = charRun.text();
                          }
                      }
                  }
              }
              bufIStream.close();
              bufIStream = null;
  
              outputFile = new File(outputFilename);
              fileOStream = new FileOutputStream(outputFile);
              bufOStream = new BufferedOutputStream(fileOStream);
              document.write(bufOStream);
          }
          catch(IOException ex) {
              System.out.println("Caught an: " + ex.getClass().getName());
              System.out.println("Message: " + ex.getMessage());
              System.out.println("Stacktrace follows.............");
              ex.printStackTrace(System.out);
          }
          finally {
              if(bufIStream != null) {
                  try {
                      bufIStream.close();
                      bufIStream = null;
                  }
                  catch(IOException ex) {
                  }
              }
              if(bufOStream != null) {
                  try {
                      bufOStream.flush();
                      bufOStream.close();
                      bufOStream = null;
                  }
                  catch(IOException ex) {
  
                  }
              }
          }        
      }
      public static void main(String[] args) {
          Prueba replacer = new Prueba();
  
          HashMap&lt;String, String&gt; searchTerms = new HashMap&lt;&gt;();
          searchTerms.put("search term 1", "replacement term 1");
          searchTerms.put("search term 2", "replacement term 2");
          searchTerms.put("search term 3", "replacement term 3");
          searchTerms.put("search term 4", "replacement term 4");
  
          replacer.searchAndReplace("Document.doc", // Source Document
                  "Replaced Document.doc", // Result Document
                  searchTerms);                             // Search/replacement items
      }
  }
  </code></pre>
  
  <p>When I execute my code, the error is the next: </p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/OldFileFormatException
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)  at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at Prueba.searchAndReplace(Prueba.java:44)
      at Prueba.main(Prueba.java:123)
  Caused by: java.lang.ClassNotFoundException: org.apache.poi.OldFileFormatException
      at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 14 more
  </code></pre>