0

am trying to run excel using jacob , but it keeps throwing an exception , been searching for awhile for a cause of such exception , but no good

package com.se.jaexcel;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JExcel {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ActiveXComponent xl = new ActiveXComponent("Excel.Application");

    }
}

the exception is

Exception in thread "main" com.jacob.com.ComFailException: Can't QI object for IDispatch
    at com.jacob.com.Dispatch.createInstanceNative(Native Method)
    at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
    at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
    at com.se.jaexcel.JExcel.main(JExcel.java:14)
Exorcismus
  • 2,243
  • 1
  • 35
  • 68

1 Answers1

0

you are not loading any native dll. In the example below c:\myapp\lib contains jacob-1.18-M2-x64.dll and jacob-1.18-M2-x86.dll. If you do not want to load them from a static location, see http://www.javaquery.com/2013/12/getting-started-with-jacob-example-with.html to see how you can load the DLL's from a resource.

private static void loadLibrary(final String appDir) throws IOException {
        final String libFile = "amd64".equals(System.getProperty("os.arch")) ? 
                  "/jacob-1.18-M2-x64.dll" : "/jacob-1.18-M2-x86.dll";
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, 
                           Paths.get(appDir, "lib", libFile).toString());
        LibraryLoader.loadJacobLibrary();
    }


public static void main(String[] args) {
        loadLibrary("c:\\myapp");
        ActiveXComponent xl = new ActiveXComponent("Excel.Application");

    }
Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49