COM4J updates

It’s been a while, but I’ve posted a new version of COM4J. COM4J is a library that lets you talk to Windows COM components. Unlike similar libraries lika jacob, which makes you feel like you are working with reflection, COM4J is designed to work with type-safe annotated interfaces, which makes you feel like you are working with Java libraries. COM4J is also built on top of vtable invocation, not on IDispatch, so it can work with components without the dual interface support (boy those words bring back memories!)

I use this library in Jenkins, among other places, to provide a better native integration.

The major change in this version is that it finally has 64bit Java support. The original work was contributed in 2011, but I’ve never cut a release out of it officially. It contains a number of bug fixes, additional conversions support. The code is now on GitHub, and the website is moved to here.

35 thoughts on “COM4J updates”

  1. Great News! Thank you! :)

    Maybe we should post on the “old” com4j java.net mailing list and kinda redirect users from the old project site to your new one?

  2. Yes, I should post the release announcement. I still consider that mailing list THE mailing list, as I don’t have anywhere else to host them.

  3. Hello Kohsuke !
    Sorry to say, but I have problem using com4j. Whenever I try to run tlb import (regardless if I try to run with wshom.ocx or WIA TLB) I got the following error:
    F:\com4j\kohsuke-com4j-b5143ce\tlbimp\lib>java -jar tlbimp.jar -o wia -p wia %WINDIR%\system32\wiaservc.dll
    Exception in thread “main” java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:503)
    at com4j.ComThread.execute(ComThread.java:220)
    at com4j.Task.execute(Task.java:25)
    at com4j.COM4J.loadTypeLibrary(COM4J.java:322)
    at com4j.tlbimp.driver.Lib.getLibid(Lib.java:90)
    at com4j.tlbimp.driver.Driver.addLib(Driver.java:38)
    at com4j.tlbimp.driver.Main.doMain(Main.java:123)
    at com4j.tlbimp.driver.Main.main(Main.java:56)

  4. Hi, I’m currently trying to upgrade à Java solution using ABBY OCR from Windows XP 32bit to Windows 7 64bit. A bridge using com4j exist between the two. Generation of the new interphases works well, but use it or the older generate this stack trace:
    com4j.ComException: 8000ffff Défaillance irrémédiable : Erreur programme interne:
    .\src\RegExpPreprocessor.cpp, 53. : .\invoke.cpp:517
    at com4j.Wrapper.invoke(Wrapper.java:166)
    at $Proxy97.recognizeNextDocument(Unknown Source)
    at net.atos.FraudeDocumentaire.engine.EngineAbbyy.analyseDocument(EngineAbbyy.java:202)
    at net.atos.FraudeDocumentaire.engine.ThreadIdentification.run(ThreadIdentification.java:40)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: com4j.ComException: 8000ffff Défaillance irrémédiable : Erreur programme interne:
    .\src\RegExpPreprocessor.cpp, 53. : .\invoke.cpp:517
    at com4j.Native.invoke(Native Method)
    at com4j.StandardComMethod.invoke(StandardComMethod.java:35)
    at com4j.Wrapper$InvocationThunk.call(Wrapper.java:340)
    at com4j.Task.invoke(Task.java:51)
    at com4j.ComThread.run0(ComThread.java:153)
    at com4j.ComThread.run(ComThread.java:134)

    Have you any idea where problem come from ? Did you think it’s ABBYY internal faillure or com4j?

    Thanks for reading.

  5. can you please provide a simple example of putting an array into an excel range, I’ve tried many things and cannot get this to work….

    Object[][] aSquare = {{“1″,”2″},{“3″,”4″}};

    Range aR = app.getRange(“A1″, Variant.getMissing());
    aR = aR.getResize(2, 2);
    aR.setValue2(aSquare);

  6. I am getting the issue with 64 Bit. It obsolutely worked fine for me with 32 Bit. I tried to load the dll explicitly using

    System.load(“D:/com4j-amd64.dll”);

    The dll is loaded successfully but still I am getting

    com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153

    Do any one have any clue? Its greatly appreciated.

  7. In detail the error is happening at COM4J.java

    Line 117 : Wrapper.create( intf, Native.createInstance(clsid,clsctx,iid.v[0],iid.v[1]) );

    Hi Kohsuke any insight about this problem is greatly appreciated.

  8. Hello Kohsuke,

    I am getting same error as Siva Gudur, any suggestions to resolve this ?

    Thanks,
    Nilesh

  9. Hello,
    I tried to run a word macro with one parameter.

    app.application().run(“Modul1.Makro1″,”Norbert”, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);

    but I get the message: “illegal number of parameters.”.

    Thank you

  10. Hi,

    I am getting the error for Import statements of word.

    import word.Window;
    import word._Application;
    import word._Document;
    import word.events.ApplicationEvents2;

    Kindly clarify me on the jar files need to be added to Java build path to resolve these import statements error.

    Thanks in Advance!!

    Regards,
    Sathish.

  11. @kams62 I was able to fix it by changing my JRE to 32bit instead of 64bit since my dll was a 32bit dll. But now i run into the same problem when i deploy my application with bundled 32bit JRE. Not sure if i did something in addition to changing the JRE to 32 bit.

  12. @kams62 – I am trying to connect HP QC from Java layer. I run into below error
    “com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153 ”

    The above error disappears if I run 32bit JVM but re-occurs if I compile my code using 64bit JVM

    In http://com4j.kohsuke.org/deployment.html page you have mentioned that the latest com4j.jar does handle bot 32bit as well as 64bit, also com4j-x64.jar is included as part of com4j.jar. I don’t see the com4j-x64.jar being bundled.

    Could you please throw some light.

    Thanks for your support.

  13. We are using your package to interface with a com dll from Milliman. One of their methods can return a Double, Float, or a double[]. The Java side return value is an Object but the double[] is not coming back. We get a null instead. I love your product and would like to continue to use it. Can you give us any suggestions on how to proceed? Maybe you could point us to what module needs to be changed. We are great Java programmers but have not programmed in C in years. Thanks for you help.

  14. Will this com4j.jar file compatible with MAC OSX? .dll on MAC doesn’t make sense, but that is why I am asking this question.

  15. I use com4j on Windows 2008 Server R2. It is a 64bit OS, a 32bit Office and a 64bit JRE are running on it. My problem: My application runs as service under local system account. It has to open a word document by calling wordApp.documents().open(…). The open-method returns null!

    If I run the application by start script everything works fine but not as service. I tried to install Office as 64bit and the service surprisingly works fine, too. What is the problem?

    I suppose the internal selected dll plays a role used by com4j?!

    Best regards Danny

  16. I have a query where my integration with QC is with MAC machine. it was failing constantly with the below exception, even though the libs and everything are referenced to the current project but still it didnt help much,
    [junit] Exception in thread “Com4J shutdown hook” java.lang.NoClassDefFoundError: Could not initialize class com4j.COM4J
    [junit] at com4j.COM4J$3.run(COM4J.java:476)

  17. i am getting error like : Exception in the thread “main” com4j,ExecutionException: java.lang.NoClassDefFoundError: office/Assistant . Can somebody help me why am getting that error. Thanks in advance

  18. Hello Kohsuke Kawaguchi

    Thank you for these opensource project.
    I’m using it to communicate to OpenDSS (http://sourceforge.net/projects/electricdss/). It works great for most of the commands. But when it’s a vector of real and imaginary values, it generate the following code:
    ///////////////////////////////////////////////////////////////////////////
    /**
    *
    * Complex array of all bus, node voltages from most recent solution
    *
    *
    * Getter method for the COM property “AllBusVolts”
    *
    * @return Returns a value of type java.lang.Object
    */

    @DISPID(11) //= 0xb. The runtime will prefer the VTID if present
    @VTID(17)
    @ReturnValue(type=NativeType.VARIANT)
    java.lang.Object allBusVolts();
    ///////////////////////////////////////////////////////////////////////////
    Unfortunately it doesn’t work, returning null.
    Does anyone knows how to fix these?

  19. Hello Kaushik,

    I am getting the below error when I try to connect from windows 7 64 bit.

    Exception in thread “main” com4j.ExecutionException: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.ComThread.execute(ComThread.java:203)
    at com4j.Task.execute(Task.java:25)
    at com4j.COM4J.createInstance(COM4J.java:97)
    at com4j.COM4J.createInstance(COM4J.java:72)
    at com.qc.ClassFactory.createTDConnection(ClassFactory.java:16)
    at com.test.Test.getALMConnection(Test.java:26)
    at com.test.Test.main(Test.java:43)
    Caused by: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.Native.createInstance(Native Method)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:117)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:104)
    at com4j.Task.invoke(Task.java:51)

  20. Any body has resolution to this.

    Exception in thread “main” com4j.ExecutionException: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.ComThread.execute(ComThread.java:203)
    at com4j.Task.execute(Task.java:25)
    at com4j.COM4J.createInstance(COM4J.java:97)
    at com4j.COM4J.createInstance(COM4J.java:72)
    at com.qc.ClassFactory.createTDConnection(ClassFactory.java:16)
    at com.test.Test.getALMConnection(Test.java:26)
    at com.test.Test.main(Test.java:43)
    Caused by: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.Native.createInstance(Native Method)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:117)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:104)
    at com4j.Task.invoke(Task.java:51)

  21. Hi ,
    I am getting following exception in 64 bit .

    execute==com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153

    i don’t know what to do on this issue please help me
    Thanks advance .

  22. I’m using com4j to access an ocx com library. All the requests work properly, but one of the incoming events is mapped as:

    public void stateException(Holder sState, Holder sMethod, Holder sMessage)

    The variant to Holder conversion fails with the following error:

    com4j.ComException: 80004005 Unable to convert VARIANT 40204 to the com4j.Holder : Unspecified error : .\variant.cpp:46

    This leads to the VM crashing with exception EXCEPTION_ACCESS_VIOLATION (0xc0000005) (see following log)

    If I remove the offending method the program runs properly. Any idea why this could be happening?

  23. Hi,
    I am trying to record a .ICA File using JMeter and when i click on record i am also getting same error.

    com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153

    @Kohsuke: Let us know if you are aware of how to fix this or atleast let us know if there is some work inprogress on this, based on which we can make some decisions whether to proceed with POC or not.

    Thanks,
    Madhu

  24. So I was able to figure this out (finally). What I did was register the .all that is using com4j (and it’s codebase) with RegAsm through the .Net that was used.

    In an elevated command prompt, navigate to where your dll that is using com4j and use the following command:

    Be sure you are using the correct .Net framework for the dll that you are using.

    C:/Windows/Microsoft.NET/Framework64/v4.0.30319/RegAsm.exe .dll /codebase

  25. I am getting the following exception.What could be the cause of it?

    Exception in thread “main” com4j.ExecutionException: com4j.ComException: 80029c4a LoadTypeLib failed : Error loading type library/DLL. : .\com4j.cpp:398
    at com4j.ComThread.execute(ComThread.java:203)
    at com4j.Task.execute(Task.java:25)
    at com4j.COM4J.loadTypeLibrary(COM4J.java:322)
    at com4j.tlbimp.driver.Lib.getLibid(Lib.java:90)
    at com4j.tlbimp.driver.Driver.addLib(Driver.java:38)
    at com4j.tlbimp.driver.Main.doMain(Main.java:123)
    at com4j.tlbimp.driver.Main.main(Main.java:56)
    Caused by: com4j.ComException: 80029c4a LoadTypeLib failed : Error loading type library/DLL. : .\com4j.cpp:398
    at com4j.Native.loadTypeLibrary(Native Method)
    at com4j.COM4J$2.call(COM4J.java:324)
    at com4j.COM4J$2.call(COM4J.java:322)
    at com4j.Task.invoke(Task.java:51)
    at com4j.ComThread.run0(ComThread.java:153)
    at com4j.ComThread.run(ComThread.java:134)

  26. Hi

    My code is working in eclipse but while running through jar file i am getting below error message and batch file.

    1) Batch file

    @echo off

    chdir /d C:\\Desktop\\alm
    java -cp “WC.jar;C:\Automation\Jars\*” test.script

    com4j.ExecutionException: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.ComThread.execute(ComThread.java:203)
    at com4j.Task.execute(Task.java:25)
    at com4j.COM4J.createInstance(COM4J.java:97)
    at com4j.COM4J.createInstance(COM4J.java:72)
    at com.mercury.qualitycenter.otaclient.ClassFactory.createTDConnection(Unknown Source)
    at util.QcIntegration.ALMUpdatedTC(QcIntegration.java:47)
    at testscript.DriverScript.testApp(DriverScript.java:860)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:105)
    at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:62)
    at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:49)
    at testscript.DriverScript.main(DriverScript.java:925)
    Caused by: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153
    at com4j.Native.createInstance(Native Method)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:117)
    at com4j.COM4J$CreateInstanceTask.call(COM4J.java:104)
    at com4j.Task.invoke(Task.java:51)
    at com4j.ComThread.run0(ComThread.java:153)
    at com4j.ComThread.run(ComThread.java:134)

    register :OTAClient.dll through cmd.

    can you please provide any solution?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>