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());
System.exit(1);
}

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”);
producer.send(message);

producer.close();
qSession.close();
qConn.close();

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:

com.ibm.mq.jar
com.ibm.mqjms.jar
com.ibm.ws.webservices.thinclient_6.1.0.jar
dhbcore.jar
ws_runtime.jar

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.