即使添加了jar文件,也无法编译java项目?

时间:2014-01-29 16:58:12

标签: java eclipse jms

我一直在使用jms服务构建项目。我在eclipse中创建了这个项目。

Project name : JmsTest
src->jmstestclient(package)->SimpleProducer.java(class)

SimpleProducer.java的代码如下

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


public class SimpleSynchConsumer {
    /**
     * Main method.
     *
     * @param args     the destination name and type used by the
     *                 example
     */
    public static void main(String[] args) {
        String destName = null;
        Context jndiContext = null;
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination dest = null;
        MessageConsumer consumer = null;
        TextMessage message = null;

        if (args.length != 1) {
            System.out.println("Program takes one argument: <dest_name>");
            System.exit(1);
        }

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

        /*
         * Create a JNDI API InitialContext object if none exists
         * yet.
         */
        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.
         */
        try {
            connectionFactory = (ConnectionFactory) jndiContext.lookup(
                    "jms/ConnectionFactory");
            dest = (Destination) jndiContext.lookup(destName);
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create consumer, then start message delivery.
         * Receive all text messages from destination until
         * a non-text message is received indicating end of
         * message stream.
         * Close connection.
         */
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(dest);
            connection.start();

            while (true) {
                Message m = consumer.receive(1);

                if (m != null) {
                    if (m instanceof TextMessage) {
                        message = (TextMessage) m;
                        System.out.println("Reading message: " +
                            message.getText());
                    } else {
                        break;
                    }
                }
            }
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}

我已下载了javax.jms-3.1.2.2 jar文件,并将此文件放在java文件夹下的bin \ lib中。但我收到以下错误。

The import javax.jms cannot be resolved. 
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    ConnectionFactory cannot be resolved to a type
    Connection cannot be resolved to a type
    Session cannot be resolved to a type
    Destination cannot be resolved to a type
    MessageConsumer cannot be resolved to a type
    TextMessage cannot be resolved to a type
    ConnectionFactory cannot be resolved to a type
    Destination cannot be resolved to a type
    Session cannot be resolved to a variable
    Message cannot be resolved to a type
    TextMessage cannot be resolved to a type
    TextMessage cannot be resolved to a type
    JMSException cannot be resolved to a type
    JMSException cannot be resolved to a type

    at jmstestclient.SimpleSynchConsumer.main(SimpleSynchConsumer.java:51)

它指出导入javax.jms行的错误

目前我没有JEE或j2ee。我只安装了jdk而不是j2ee。我是否必须安装j2ee才能连接JMS服务器。

我怎样才能使这个程序可编辑。

1 个答案:

答案 0 :(得分:0)

在应用程序的类路径中添加jar。