有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在使用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)

它指出了import javax上的错误。jms行

目前我没有JEE或j2ee。我只安装了jdk,没有安装j2ee。我需要安装j2ee来连接JMS服务器吗

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


共 (1) 个答案

  1. # 1 楼答案

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