1

Currently trying to access a type library file from JAVA, I have tried the following with corresponding errors:

1- Com2Java: I receive this Error Each time I try to connect to my application:

Minidumps are not enabled by default on client versions of Windows

2- Com4j: It produces only interfaces and Couldn't understand how to use them (I can't find any classes, just interfaces)

3- After a small search, found out about Visual J++ but couldnt download it coz it was discontnued.

Could anyone give advice?

Thank you

Community
  • 1
  • 1
Peter
  • 79
  • 1
  • 12

1 Answers1

0

I have not used Com2Java or Com4j before, but a long time ago I used a library called JavaCOMBridge (https://sourceforge.net/projects/jacob-project/).

The version of JavaCOMBridge I used cannot handle multiple inheritance, and I don't see how there can be a good way to do it.

Forget about Visual J++. It's an abomination created by Microsoft and was sued into oblivion.

If you are experienced in both C and Java, and the amount of APIs you have to bridge is not large, I'd recommend using JNI directly.

Edit Here's an example using Excel:

package test;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JaCoBTest {
        public static void main(String[] args) {
                String EXCEL_FILE = "FullPathOfAnExcelFile.xlsx";
                // Using Excel as an example
                ActiveXComponent app = new ActiveXComponent("Excel.Application");
                // Modify a property, to show Excel window
                app.setProperty("Visible", true);
                // Get Excel workbook object
                Dispatch workbook = app.getProperty("Workbooks").toDispatch();
                // Call method, to open an Excel file
                Dispatch.call(workbook, "Open", new Variant(EXCEL_FILE), new Variant("1"));
                // Wait for 5 seconds
                try {
                        Thread.sleep(1000);
                } catch (InterruptedException iex) {
                        iex.printStackTrace();
                }
                // Close Excel without saving
                workbook.call(workbook, "Close");
                // Close is supposed to have three optional parameteters, but the line below is not working
                //workbook.call(workbook, "Close", new Variant(false), Variant.DEFAULT, Variant.DEFAULT);
                // Close Excel
                Dispatch.call(app, "Quit");
        }
}

There is one problem in the above code - I cannot get optional parameters to work. The function Workbook.Close is supposed to take three optional parameters, but the call always fail with invalid number of parameters.

I've also located the web page I used back then: http://danadler.com/jacob/ The above page contains a link to a FAQ but it's slightly outdated.

KC Wong
  • 2,410
  • 1
  • 18
  • 26
  • Firstly, I'd like to thank you for your Answer, I'm currently reading more about JNI and JACOB is definitely next on my list (or vice versa if you recommend so) Unfortunately, I'm not experienced in either (I can use C and Java but really on the basic level). I'm just a Chemical Engineer who uses programming to achieve an ultimate goal, So I miss so many technicalities... – Peter Oct 05 '16 at 07:50
  • If there is anyway that u can give me a head start (gimme an article, reference, book, anything..) to understand how COM and/or JACOB works, I would be really grateful. – Peter Oct 05 '16 at 07:59
  • It's been a very long time (over 10 years), so I will need a bit of time to awaken my memory about JaCoB. If you are not experienced in C and Java, then doing JNI is not recommended as it requires you to be very careful about memory management and threading. I'll try to get an example ready in a few days. – KC Wong Oct 05 '16 at 09:37
  • I can't thank you enough.. I'm currently studying JACOB and trying to get my application CLSID using OLE viewer... I'm looking forward to your example. – Peter Oct 06 '16 at 00:27
  • I've updated my answer with an example and links to the resource I used back then. – KC Wong Oct 07 '16 at 06:34