Writing a standalone JMS Java Client to connect to Websphere MQ 6.0 via Websphere Application Server 6.1

Somehow, when I tried to write a standalone client I kept coming across the following exception
java.lang.ClassCastException: javax.naming.Reference incompatible with javax.jms.ConnectionFactory “.

I followed intructions from here to setup the Queue, after which I created a simple java client to post a message to the Queue:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, “com.ibm.websphere.naming.WsnInitialContextFactory”);
env.put(Context.PROVIDER_URL, “iiop://localhost:2809”);

Context jndiContext = null;
try {
jndiContext = new InitialContext(env);
} catch (NamingException e) {

System.out.println(“Could not create JNDI API context: ” + e.toString());

ConnectionFactory connectionFactory = (ConnectionFactory)  jndiContext.lookup(“jms/JMSExampleConnectionFactory”);
Connection qConn = connectionFactory.createConnection();
Session qSession = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) jndiContext.lookup(“jms/JMSExampleQueue”);

MessageProducer producer = qSession.createProducer(q);
TextMessage message = qSession.createTextMessage();
message.setText(“test message”);


Even after switching to the IBM JRE and adding the relevant Jars (as suggested in most places.) the program kept throwing the ClassCast Exception. Just in case you are wondering, the relevant Jars ( found in <Websphere Installation Path>/runtimes/ and <Websphere Installation Path>/lib/WMQ/java/lib/ ) are:


After googling and reading through lot of forum threads I finally stumbled on the solution. To resolve this you also need to add the <Websphere Installation Path>/plugins/com.ibm.ws.runtime_6.1.0.jar to the classpath.  Seems simple enough, but it took me ages to find. Hence the post.


52 thoughts on “Writing a standalone JMS Java Client to connect to Websphere MQ 6.0 via Websphere Application Server 6.1

  1. Did you switch your JRE on the Server or on the Client?
    I tried everything on the client and finally gave up!!! I’m running things as a servlet and it looks to be working fine..

  2. I had the same problem in MQ 7.0.1 and solved it by adding the following jars in the CLASSPATH of my Java client:


    Hope this helps someone someday! 🙂

  3. Hi,
    I am trying to use JNDI in my standalone java client to connect to the websphere MQ JMS provider.We don’t have any JNDI on JMS provider side.
    So I was wondering from where do we get this connectionfactory name.
    I am using Queue and Topic names directly and look these up successfully but not sure How I should lookup for connection factory.


  4. Great items from you, man. I’ve understand your stuff prior to and you’re just extremely great. I really like what you have received right here, certainly like what you are stating and the way wherein you assert it. You make it entertaining and you continue to take care of to stay it sensible. I can not wait to read far more from you. This is actually a tremendous web site.

  5. Thanks for this great post, it even helped me 3 years later to get this to work on WAS 7.0. The directory then is:

    This is for an Websphere BPM 7.5 installation.

  6. Hi,
    I would like to say thank you for your posting, which finally helped me to establish connection from Cast Iron to Websphere App Server (7.0) – Message Queue. (in order for Cast Iron to post or pull messages from the Message Queue)
    I needed to include the following jars:


  7. Hi , I have a question on including the JMS provider specific jars included in the code. I just want to develop a generic Java agent to talk to a JMS provider

    New to JMS and thought I could code with just standard Java SE (for JNDI) and javax,jms (not included in SE library), I could connect to ANY JMS provider.
    I have not tried IBM MQ, but I tried on OPEN JMS. The code compiles OK with just the jms1-1.jar (containing javax.jms). I use the following to pass the connection properties

    Hashtable properties = new Hashtable();
    properties.put(Context.PROVIDER_URL, “tcp://localhost:3035/”);

    Looks pretty typical. Until I try to send a message to the OPEN JMS, I got javax.naming.NoInitialContextException: Cannot instantiate class: org.exolab.jms.jndi.InitialContextFactory. It means the JMS provider cannot find the class. The problem get resolved after I add ALL the jar files from the LIB folder. It make no difference in compiling, but now I can access the JMS provider.
    After seen Katrin’s response, It seems that a Java client DO need to include vendor specific jars to be able to connect to that provider, even though including those jars are the only thing a Java developer needs to do.

    Anyone can confirm on with that?

    It seems there is NO way to avoid it. I initial try to stay vendor neutral, but now I have to consider the specific jars.

  8. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get got an edginess over that you
    wish be delivering the following. unwell unquestionably
    come further formerly again as exactly the same nearly very often inside case you shield
    this increase.

  9. My spouse and I stumbled over here from a different web address and thought I may as well check
    things out. I like what I see so now i’m following you. Look forward to looking into your web page repeatedly.

  10. Greetings from California! I’m bored at work so I decided to check out
    your site on my iphone during lunch break. I enjoy the information you present here and can’t
    wait to take a look when I get home. I’m amazed at how quick
    your blog loaded on my cell phone .. I’m not even using WIFI, just
    3G .. Anyways, amazing blog!

  11. I seriously love your site.. Excellent colors & theme.
    Did you create this amazing site yourself? Please reply back as I’m looking to
    create my own personal website and want to find out where you got this from or just what the theme is
    named. Kudos!Meez cheats november 2013

  12. Use a gutter guard to prevent ice from building up inside, as the access weight could break it.

    A good place to start, before ever climbing a ladder (which, by the way, make sure to always
    use precautions when climbing a ladder and have a second person spot you if possible), you can
    give your roof a once over from the ground. Of course, we have
    all heard the stories of people who try to do their own work and
    end up spending more money in the long run.

  13. I like the valuable info you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.
    I’m quite certain I will learn a lot of new stuff right here!
    Good luck for the next!

  14. Pingback: How To Fix Error 1310 Error Writing To File Java Errors - Windows Vista, Windows 7 & 8

  15. Pingback: Fix Java Server Error Windows XP, Vista, 7, 8 [Solved]

  16. Pingback: Fix The Server You Are Connected To Is Using Windows XP, Vista, 7, 8 [Solved]

  17. Pingback: Fix Mpich2 Error While Connecting To Host Windows 7 Windows XP, Vista, 7, 8 [Solved]

  18. Pingback: Fix Top Eleven Error Connecting To Server Windows XP, Vista, 7, 8 [Solved]

  19. Pingback: How To Write A Check Nationwide | HOW TO

  20. Pingback: How To Write A Check To Scottrade | HOW TO

  21. Pingback: How To Write A Check No/100 | HOW TO

  22. Pingback: How To Write A Check To Verizon | HOW TO

  23. Pingback: How To Write A Check Uk Barclays | HOW TO

  24. Pingback: How To Write A Check For 1500 | HOW TO

  25. Pingback: How To Write A Check.com | HOW TO

  26. Pingback: How To Write A Check Using Words | HOW TO

  27. Pingback: How To Write A Check Us | HOW TO

  28. Pingback: How To Write A Check Out With Cents | HOW TO

  29. Pingback: How To Write A Check Thousand | HOW TO

  30. Pingback: How To Write A Check One Thousand | HOW TO

  31. Pingback: How To Write A Check In French | HOW TO

  32. Pingback: How To Write A Check Receipt | HOW TO

  33. Hi,

    I am getting below the exception while running attched code:

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, “com.ibm.websphere.naming.WsnInitialContextFactory”);
    env.put(Context.PROVIDER_URL, “iiop://”);
    //env.put(“java.naming.provider.url”, “iiop://”);

    Context ctx = null;
    try {
    // get the initial context
    System.out.println(“Going to initiate context”);
    ctx = new InitialContext(env);
    System.out.println(“Context initiated”);
    } catch (NamingException e) {
    System.out.println(“Could not create JNDI API context:” + e.toString());
    // lookup the queue connection factory
    QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(“jms/maximo/int/cf/intcf”);

    // lookup the queue object
    Queue queue = (Queue) ctx.lookup(“jms/maximo/int/queues/cqin”);

    // create a queue connection
    QueueConnection queueConn = connFactory.createQueueConnection();

    // create a queue session
    QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);

    // create a queue sender
    QueueSender queueSender = queueSession.createSender(queue);

    // create a simple message to say “Hello”
    TextMessage message = queueSession.createTextMessage(“Hello”);

    // send the message

    // print what we did
    System.out.println(“sent: ” + message.getText());

    // close the queue connection

    Exception in thread “main” java.lang.NoSuchMethodError: com.ibm.ws.bootstrap.WsLogManager.isHpelEnabled()Z
    at com.ibm.ejs.ras.Tr.(Tr.java:294)
    at com.ibm.websphere.naming.WsnInitialContextFactory.(WsnInitialContextFactory.java:80)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
    at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.(Unknown Source)

    Could anyone help me to resolve this issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s