为什么java代码在调用传递参数的类时正确编译并抛出错误?

为什么java代码在调用传递参数的类时正确编译并抛出错误?,java,jms,Java,Jms,我一直在关注这个url 我已经创建了连接工厂、队列和主题。我一直在使用上面url中给出的源代码来连接JMS。当我运行下面的代码时,没有任何编译错误,但是在传递参数时代码不会运行 import javax.jms.*; import javax.naming.*; public class SimpleProducer { public static void main(String[] args) { final int NUM_MSGS; if ((

我一直在关注这个url 我已经创建了连接工厂、队列和主题。我一直在使用上面url中给出的源代码来连接JMS。当我运行下面的代码时,没有任何编译错误,但是在传递参数时代码不会运行

import javax.jms.*;
import javax.naming.*;

public class SimpleProducer {
    public static void main(String[] args) {
        final int NUM_MSGS;

        if ((args.length < 1) || (args.length > 2)) {
            System.out.println("Program takes one or two arguments: " +
                "<dest_name> [<number-of-messages>]");
            System.exit(1);
        }

        String destName = new String(args[0]);
        System.out.println("Destination name is " + destName);

        if (args.length == 2) {
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }

        Context jndiContext = null;

        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API context: " + e.toString());
            System.exit(1);
        }

        /*
         * Look up connection factory and destination.  If either
         * does not exist, exit.  If you look up a
         * TopicConnectionFactory or a QueueConnectionFactory,
         * program behavior is the same.
         */
        ConnectionFactory connectionFactory = null;
        Destination dest = null;

        try {
            connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/ConnectionFactory");
            dest = (Destination) jndiContext.lookup(destName);
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create producer and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        Connection connection = null;
        MessageProducer producer = null;

        try {
            connection = connectionFactory.createConnection();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(dest);

            TextMessage message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is message " + (i + 1));
                System.out.println("Sending message: " + message.getText());
                producer.send(message);
            }

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            producer.send(session.createMessage());
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}
我必须在glassfish服务器的jndi.properties配置文件中配置任何东西吗。目前jndi.properties最终版本

java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory

您提供的URL有一节“创建JMS管理对象”。您应该按照这些步骤正确配置测试环境

本教程将教您如何使用web界面进行配置,但是您应该能够在服务器上找到配置文件,以便稍后将相同的数据应用于Eclipse环境


Glassfish有一个类似的web控制台。您应该能够以与本教程类似的方式创建连接工厂。

成功编译的代码不一定会运行:)您希望编译错误在哪里?这是有效的Java,您只是没有为执行时间正确配置环境…@JonSkeet我怎么可能配置路径。我目前还不熟悉java和JMS。我不知道,我自己也没有使用过JMS。我建议您阅读一本好的JMS/JNDI教程。但令我惊讶的是,有些东西可以编译,但在执行时仍然失败。这就是我的观点——你似乎预料到编译会失败。。。你认为应该发生在哪里?
java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory