JMS 和 ActiveMQ 异常

发布于 2025-01-02 12:49:58 字数 1736 浏览 0 评论 0原文

我正在尝试使用 JMS 和 ActiveMQ 的学校项目。

我从 O'Reilly 的书“Java Message Service 2nd Edition Jun 2009”中复制了代码块。它使用发布和订阅方法,实际上是一个小型聊天,连接到该主题的每个人都可以向每个人发送消息,每个人都可以看到其他人的消息。我编译程序,一切正常,我尝试运行它,它给了我以下异常:

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]

我发现这个问题可能是由于 2 个原因:

  1. activemq-all-5.2.0.jar 没有添加到类路径中。< br> 但将其添加到类路径(EnvironmentVariables->选择 ClassPath->编辑并添加以下内容:“D:\Programming\JMS\ActiveMQ\apache-activemq-5.2.0”(这就是您添加的方式?!? !)
  2. jndi.properties 文件未正确定义或尚未添加到类路径中。
    但我创建了它并将其文件夹添加到类路径中。以下是它包含的内容:

    java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
    java.naming.provider.url = tcp://localhost:61616
    java.naming.security.principal=系统
    java.naming.security.credentials=经理
    连接工厂名称 = TopicCF
    主题.主题1 = jms.主题1
    

问题是什么?我已经尝试了很多年才能让它发挥作用。我做错了什么吗? :(

jndi.properties 文件路径重要吗?或者它只需要放在类路径中,然后从这里就可以找到它?

我还从 bin 文件夹 D:\Programming\JMS\ActiveMQ\apache 运行了 activemq.bat- activemq-5.2.0\bin\

[编辑]-------------------- 所以它在 Eclipse 中可以工作,但是 现在我已经在环境变量中正确添加了 .jar 文件,并且我已经从 Windows 的 cmd 运行客户端。它没有给出任何错误,当我在 Eclipse 的控制台中写入时,它出现在 cmd 控制台中,一切正常,但是当我尝试在 cmd 中写入时,它在这一行给出错误:

publisher.publish(message);

并且它说

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

Any ideas?我真的很希望能够在 CMD 中运行它。 :(

- - - - - - - - - - -[/编辑]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2025-01-09 12:49:58

好吧,我现在在 Linux 上,但我打赌它必须是:

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

另外,如果你用 Eclipse 运行它并转到项目 ->构建路径和这个 jar 应该不会有任何问题。无论如何,你可以发布 CLASSPATH 变量吗?

编辑

如果你不能帮助我,我也不能帮助你。这与任何其他未来的问题或一般工作有关,提供详细信息 - 它总是有帮助的。如果您提供在 CMD 中运行的确切命令以及发生这种情况的类的代码,将会非常有帮助。

java.lang.NoSuchMethodError

通常意味着 jar 已就位,类也已就位,但方法尚未就位。当您使用一个版本的 jar 进行编译并在运行时提供一个已删除此方法的 jar 时,就会发生这种情况,因此 JRE 找不到它并抛出错误。

我刚刚在我的电脑上测试

我不明白为什么它对你不起作用,但对我有用。这是我的类:

 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();
        }
    }
}

如果我从 Eclipse 运行它并在 Maven 中使用一个依赖项,那么一切都很好:

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

然后我使用 javajavac

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 

请注意,我添加的唯一内容是两个罐子。

然后 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

我在类路径中添加了一些所需的 jar 并运行它 - 它工作得很好。

干杯,尤金。

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.

银河中√捞星星 2025-01-09 12:49:58

我遇到了同样的问题,它是我的属性配置末尾的一个空格(或看起来是一个空格)。

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

另请注意,您不必将 jar 文件嵌入到客户端代码中。只需将 activemq-all 作为 Maven 依赖项包含进来也可以。

I ran into the same issue and it was a space (or what appeared to be a space) at the end of my property config.

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

Also note that you don't necessarily have to embed the jar file into your client code. Simply including the activemq-all as a maven dependency will work as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文