I havent got the foggiest idea how I would approach this, but how would I go about detecting if the computer my java program was running had MS Word installed, and then to take things further could I access it via its COM interface.
Actually I should reword the question, How can I acces any COM registered program ?
Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
I suppose you would need to traverse the Registry, to find out what installed programs are on there. There's something called JNI Registry which I think can do it - Google for it. Alternatively a little bit of C programming and some JNI code would do it.
Google for "Java COM Bridge". There are a few open source and free implementations and a bunch of high-dollar commercial products. If you have C/C++ skills you could make your own custom native interfaces, too.
Since Java was designed for portability Sun didn't give us any hooks into COM or other Windows APIs. If you value COM interaction over portability, .NET might be a better choice than Java.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Mar 08, 2005
My interest in COM really comes from wanting to operate with MS Word for document generation, and probably Outlook in the future for emailing/faxing.
I have done something like that. More specifically, our application is editing word documents stored on the server through Java. The only way I know of is by using a JNI layer. Your JNI layer will of course be in C++, and it can detect whether Word is installed on the system or not.
To get into more details, when you do CoCreateInstance in C++, that call will return an error code if Word is not installed on the system. You don't have to walk through the registry yourself. That call will go to the registry, find where Word is installed and invoke it. The same goes for all COM components that you might want to use.
However, COM is not a easy beast to tame, and I would'nt go into complexities if I didn't have prior experience with Windows and COM programming. Writing multi-threaded applications in COM is very complex, and you have to remember rules regarding which kind of COM objects you can use in which threads. You couple that with the fact that usually JNI layers don't handle threading, and threading is left to the calling Java application, and you will have problems if you leave COM objects lying around in memory. The rule of thumb for novices is don't use COM objects across threads, or if you really want to be safe:- always declare COM pointers as local variables;ie; instantiate at the start of your function, and release when you leave the function. That's not too efficient because every time you instantiate a COM object, the API has to walk through the registry and possibly load apps or libraries. Plus it is so hard to debug, because I don't know any way you can step into a COM function while debugging a Java app. WHen I was doing serious Java to COM programming, I would have both JBuilder and Visual Studio up, and I would make Visual Studio connect to JBuilder. It was a mess. I don't want to get into too many details of COM in a Java related board, so, in a nutshell, either be careful or try to gain expertise in COM
That Java COM bridge sounds interesting though. It might reduce your learning curve, but I am skeptical whether any Java COM bridge can solve the threading issues inherent in COM
Needless to say, your application will not be platform-independent, but I am sure you must have thought about that. I had heard about an open-source project regarding a pure Java implementation of a a Word file reader. I think it was Jakarta POI http://jakarta.apache.org/poi/hwpf/projectplan.html But, when I was looking into it, it was'nt too mature. You might want to investigate that [ March 24, 2005: Message edited by: Jayesh Lalwani ]
Joined: Mar 08, 2005
Thanks, Some very good info there. It certainly sounds like its no walk in the park to do what I was considering, in these respects .NET would certainly make things much easier, but then take away my platform independance. I wonder if there are any java packages that allow me to easily work with Open Office. Or something similar.
Originally posted by Dave Brown: Thanks, Some very good info there. It certainly sounds like its no walk in the park to do what I was considering, in these respects .NET would certainly make things much easier, but then take away my platform independance. I wonder if there are any java packages that allow me to easily work with Open Office. Or something similar.
Doing this completely in Java will take away platform independence as well. COM is specific to Windows programs. I'm not aware of this technology being used on any other platforms, although that doesn't mean it isn't.
As for Open Office, I originally thought you would encounter similar problems. However, after visiting their website, it looks like there is some really good Java support. In particular, you may want to look at the Java UNO reference. This looks like Java classes that may be available for you to use. I don't know for sure if you have to download anything extra that doesn't come with the regular OpenOffice installation, but I wouldn't be surprised if you do. If you are interested in pursuing this further, you should poke around their website.