26278 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
I know who "Satoshi
Nakamoto" is, says
Ted Nelson
Google builds
crowdsourcing into
new Maps code stack
Google"s Native
Code browser tech
goes cross-platform
Yahoo! to "share
something special"
in New York on
Monday
Adobe"s Creative
Cloud fails at
being a cloud
NASA signs off on
sampling mission to
Earth-threatening
asteroid
US military
welcomes Apple iOS
6 kit onto its
networks
Jailed Romanian
hacker repents,
invents ATM
security scheme
Climate scientists
agree: Humans cause
global warming
MIT takes
battery-powered
robot cheetah for a
gallop
Slashdot
Wikileaks Releases
Docs Before Trial
of TPB Founder Warg
John McAfee"s
Belize Home Burns
To Ground
Amazon, Google and
Apple Won"t Need To
Pay Tax, Despite
Goverment Threats
NetBSD 6.1 Has
Shipped
Charge Your
Cellphone In 20
Seconds
(Eventually)
Yahoo! Japan May
Have Had 22 Million
User IDs Stolen
Ask Slashdot: Why
Do Firms Leak
Personal Details In
Plain Text?
Data Center
Managers Weary of
Whittling Cooling
Costs
Canadian Cellphone
Users May Get
Justice Over
Phantom Charges
Wired Writer
Imagines Google
Island
Article viewer

Calling COM objects from Java



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:Java
Search OSI about Java.More articles by dimport.
 viewed 17997 times send this article printer friendly

Digg this!
    Rate this article :
This tutorial shows how easy it is to access Microsoft COM objects using Java.

Before I start the tutorial I want to make a few things clear...

Firstly, If you want to write Windows applications that make use of COM, you should really be coding in VB or C#, however there are times when it makes sense to write your application in Java, but you really need to use some functionality provided by COM - Such as accessing a Microsoft Exchange server from a Java Web server.

Secondly, this tutorial makes use of IBM's bridge2Java technology. There are lots of alternative technologies out there, but I like IBM's solution because it's easy to live with - it just works.


Step 1 - Install Bridge2Java
This is available for download at http://www.alphaworks.ibm.com/aw.nsf/techmain/bridge2java
It's free to play with, but if you want to use it commercially there is a $1000 dollar license fee.
Unzip the file and you are ready to go.

Step 2 - generating Java COM wrappers

In your Bridge2Java directory, edit proxyall.bat to point to the COM libraries you want to use.

I am going to use ADO and Microsoft Exchange 2000, so I add the following lines to the batch file:

rmdir /q /s cdoex
proxygenBridge2Java C:winntsystem32cdoex.dll %BRIDGE2JAVA% cdoex
rmdir /q /s ado
proxygenBridge2Java "C:program filescommon filessystemADOmsado15.dll" %BRIDGE2JAVA% ado


A few things to note: cdoex.dll is the Exchange server 2000 COM object library, msado15.dll is the ADO object library.
The above lines tell bridge2Java that I want to create a package called ado based on the ado library, and a package called
cdoex based on the Exchange library.

Save the batch file and edit BASEENV.bat to point to your bridge2Java directory and Java directory. Mine looks like this:

set BRIDGE2JAVA=C:javaBRIDGE2JAVA
set JAVAJDK=C:j2sdk1.4.0
@ECHO ON
set path=%path%;%BRIDGE2JAVA%
set CLASSPATH=%JAVAJDK%libclasses;%BRIDGE2JAVA%;.


Now open a command prompt, cd into your bridge2Java directory and run baseenv.bat followed by proxyall.bat - This will create a your ado and cdoex directories, full of java source files.
You are now ready to start coding.

Coding

You will need to have your ado and cdoex directories in your source tree, you will also need to include the com.ibm.bridge2java classes on your classpath
(You will find these classes under your bridge2java directory). You will also need to do the following:

Tell the JVM where it can find the bridge2java dll - you can do this by setting a system property via the java -D flag, or in code such as

System.setProperty("java.library.path","c:javaBridge2java");


The last thing you need to do is to remember to startup and shutdown the com bridge - see the main method in the code example.

Some hints:
Check out the following code:

Fields fields = new Fields(config.get_Fields());


config.getFields returns an object, if you do this in VB, the object returned is of type Field, so the natural thing to do in java would be:
Fields fields = (Fields)config.get_Fields());


If you do this you get a class cast exception - the object returned by the getFields method is actually an INTEGER!

The rule of thumb is that if a call to a COM object returns an Object, don't try to cast it to the target type, instead create a new Object of the target type and pass the returned Object into the constructor.

If you get an error complaining about link exceptions, Java can't find bridge2Java.dll - make sure the system property is set.

If you get ole errors, make sure you
have remembered to start the bridge.

Example
The following example is a simple mail client, It demonstrates the use of ADO and CDOEX to send a mail message through an Exchange server.

/**
 * Demonstrates the use of IBM Bridge2Java with ADO and CDOEX
 */
public class Demo1
{
   public void sendMail(String[] args)
  {
     Message message = new Message();
    Configuration config = new Configuration();
      // note that casting does not work - Instead,
    // create a new object of the target type based on the returned object
    // (Returned object is actually an Integer!)
    Fields fields = new Fields(config.get_Fields());
    Field field1 = fields.get_Item(new Integer(1));
     // 3 means that you are asking mail to be sent using Exchange Server.
    fields.get_Item("http://schemas.microsoft.com/cdo/configuration/sendusing").set_Value(new Integer(3));
     fields.get_Item("http://schemas.microsoft.com/cdo/configuration/user").set_Value(args[0]);
    fields.get_Item("http://schemas.microsoft.com/cdo/configuration/password").set_Value(args[1]);
    fields.Update();
     message.set_From(args[2]);
    message.set_To(args[3]);
    message.set_Subject(args[4]);
    message.set_TextBody(args[5]);
    message.Send();
   }
 
  public static void main(String[] args)
  {
    if(args.length!=6)
    {
      System.out.println("Usage java Demo1 mailbox_username mailbox_password from to subject message");
      System.exit(0);
    }
     // need to tell the JVM where to find the bridge2Java.dll
    // we also need to have the com.ibm.bridge2java classes on the classpath
    System.setProperty("java.library.path","c:javaBridge2java");
     //initialise the bridge
    com.ibm.bridge2java.OleEnvironment.Initialize();
       Demo1 d = new Demo1();
      d.sendMail(args);
     //shut down the bridge
    com.ibm.bridge2java.OleEnvironment.UnInitialize();
  }
}



This article was originally written by pertinax

Did you like this article? There are hundreds more.

Comments:
Anonymous
2011-03-06 14:24:01
anon is a jackass.
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
goldie
using javamail i am getting excetion like this on Tue 5th May 6am
avax.mail.MessagingException: Unknown SMTP host: pop3.gmail.com; nested exception is: java.net.UnknownHostException: pop3.gmail.com at com.sun.mail.smtp.SMTPTransport.openServ er(SMTPTransport.java:1543) at com.sun.mail.smtp.SMTPTransport.protocol
kdemetter
Blog entry for Wed 11th Apr 1pm on Wed 11th Apr 1pm
lately i've been playing with Java a little . I'm working on a application that can create a list of meals for one week from a large list of meals . well , it works . just some problems with Swing not doing what i want it to do :-) http://www.os

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Java tricks by berry120

Think you really know Java? This won't test you on elaborate and never heard of APIs. It will test you on stuff that isn't always as it seems...
Java Programming - Level 1 by D-Cypell

A first level Java programming test. The test contains 10 questions on Java syntax, concepts and the API.
History and Basic Programming Knowledge by HackMaster321

A test of your knowledge of java history and programming. Tests your ability to recall java historical facts, and remember what some of javas more common commands and syntax are and do.


     
Your Ad Here
 
Copyright Open Source Institute, 2006