Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS and ActiveMQ exception

Tags:

jms

activemq

I'm trying a project for school using JMS and ActiveMQ.

I copied the block of code from O'Reilly's books "Java Message Service 2nd Edition Jun 2009". It uses the publish and subscribe method and is in fact a small chat where everyone connected to the topic can send messages to everyone and everyone can see everyone else's messages. I compile the program and everything is ok, i try to run it and it gives me the following exception:

Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.activemq.jndi.ActiveMQInitialContextFactory]

I found that this problem might be because of 2 reasons:

  1. activemq-all-5.2.0.jar is not added to classpath.
    BUT added it the classpath (EnvironmentVariables->select ClassPath->Edit and add the following: "D:\Programming\JMS\ActiveMQ\apache-activemq-5.2.0" (THIS IS HOW YOU ADD IT NO?!?!)
  2. jndi.properties file is not defined properly or has not been added to the classpath.
    BUT i CREATED IT and added it's folder to the classpath. Here is what it contains:

    java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
    java.naming.provider.url = tcp://localhost:61616
    java.naming.security.principal=system
    java.naming.security.credentials=manager
    connectionFactoryNames = TopicCF
    topic.topic1 = jms.topic1
    

What is the problem? I have tried for ages to make it work. Am i doing something wrong? :(

Does the jndi.properties file path matter? or it only has to be placed in classpath and from here it can be found?

I also ran the activemq.bat from the bin folder D:\Programming\JMS\ActiveMQ\apache-activemq-5.2.0\bin\

[Edit]--------------------- So it works in Eclipse, BUT Now i've properly added the .jar file in environment variables and i've run the client from windows's cmd. It doesn't give any errors, when i write in Eclipse's console, it appears in cmd console, everything ok, but when i try to write in cmd it gives an error at this line:

publisher.publish(message);

and it says

java.lang.NoSuchMethodError: org.apache.activemq.ActiveMQMessageProducerSupport.getDestination()Ljavax/jms/Destination;

Any ideas? I'd really like to be able to run it in CMD. :(

---------------------[/Edit]

like image 653
AndreiBogdan Avatar asked Feb 06 '12 15:02

AndreiBogdan


1 Answers

Well I'm on Linux right now, but I bet it has to be:

D:\Programming\JMS\ActiveMQ\apache-activemq-5.2.0.jar

Also, if you run it with Eclipse and go to Project -> Build Path and this jar then there shouldn't be any problems. Anyhow can you post the CLASSPATH variable?

EDIT

I can't help you if you can't help me. This is related to any other future questions or work in general, provide details - it is always helpful. Will be much helpful if you would provide the EXACT command that you are running in CMD and the code of the class where this happens.

java.lang.NoSuchMethodError

generally it means that the jar is in place, class also, BUT the method is not. It happens when you compile with one version of the jar and at runtime provide a jar where this method was removed, thus the JRE can't find it throwing the error.

I just tested on my computer

I do not understand why it does not work for you, but it does for me. Here is my class:

 package com.test;
 public class Publisher {
 public static void main(String[] args) {
try{

        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = factory.createConnection();
        ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic destination = session.createTopic("FOO.TEST");    
        TextMessage textMessage = session.createTextMessage("Sample Payload");

        TopicPublisher publisher = session.createPublisher(destination);

        publisher.publish(textMessage);

        session.close();
        connection.close();

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

Everything is fine if I run it from eclipse with one single dependency in Maven:

<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.2.0</version>

Then I do it with java and javac

javac -classpath /home/eugen/.m2/repository/org/apache/activemq/activemq-core/5.2.0/activemq-core-5.2.0.jar:/home/eugen/.m2/repository/javax/jms/jms/1.1/jms-1.1.jar  Publisher.java 

Notice that the only thing I added is the two jars.

Then java:

 java -classpath  /home/eugen/.m2/repository/org/apache/activemq/activemq-core/5.2.0/activemq-core-5.2.0.jar:/home/eugen/.m2/repository/commons-logging/commons-logging-api/1.1/commons-logging-api-1.1.jar:/home/eugen/.m2/repository/org/apache/camel/camel-core/1.5.0/camel-core-1.5.0.jar:/home/eugen/workspace/t/src/main/java/:/home/eugen/.m2/repository/javax/jms/jms/1.1/jms-1.1.jar:/home/eugen/.m2/repository/org/apache/geronimo/specs/geronimo-j2ee-management_1.0_spec/1.0/geronimo-j2ee-management_1.0_spec-1.0.jar com.test.Publisher

I added a few needed jars to the classpath and run it - it works perfectly.

Cheers, Eugene.

like image 83
Eugene Avatar answered Nov 03 '22 22:11

Eugene