Search code examples
javajacob

Error: Could not find or load main class openWordDocument


I am trying to open Microsoft Word document using jacob.

Below is code:

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class openWordDocument {
    private static final Integer wdNewBlankDocument = new Integer(0);
    private static final Variant vTrue = new Variant(true);
    private static final Variant vFalse = new Variant(false);
    private static ActiveXComponent activeXWord = null;
    private static Object activeXWordObject = null;

    public static void main(String[] args) {
    try {
    activeXWord = new ActiveXComponent("Word.Application");
    activeXWordObject = activeXWord.getObject();
    Dispatch.put(activeXWordObject, "Visible", vTrue);

    //activeXWordObject = null;

    }
    catch (Exception e) {
    quit();
    }
    }

    public static void quit() {
    if (activeXWord != null) {
    System.out.println("quit word");
    //calls the Quit method of MS Word, this will close MS Word
    activeXWord.invoke("Quit", new Variant[] {});
    ComThread.Release();
    activeXWord.release();
    System.out.println("quit word");
    }
    }

}

When I am running above code getting error Error: Could not find or load main class openWordDocument


Solution

  • It's my mistake, I added .dll file in the classpath so I am unable to compile the java file. I removed that dll file after that, jvm started compiling and able to fine the class file.