2012-04-30 179 views
0

我正在使用JMS創建一個簡單的聊天應用程序,但是我的代碼不工作,我不知道爲什麼。這是我的代碼,我與JBoss使用,所以我開的酒吧和我寫這篇文章時,我點擊啓動Eclipse給我這個錯誤消息:與JMS的簡單聊天

<pre> 
Topic or username missingjava.lang.ArrayIndexOutOfBoundsException: 0 
    at chat.pub.main(pub.java:105) 
</pre> 

and this is the code, what is the problem? 

    package chat; 

    import javax.jms.*; 
    import javax.naming.*; 
    import java.io.*; 
    import java.io.InputStreamReader; 
    import java.util.Properties; 

    public class pub implements javax.jms.MessageListener{ 
     private TopicSession pubSession; 
     private TopicSession subSession; 
     private TopicPublisher publisher; 
     private TopicConnection connection; 
     private String username; 

     /* Constructor. Establish JMS publisher and subscriber */ 
     public pub(String topicName, String username, String password) 
     throws Exception { 
      // Obtain a JNDI connection 
      Properties env = new Properties(); 
      env.put(Context.SECURITY_PRINCIPAL, "guest"); 
      env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 
      env.setProperty("java.naming.provider.url", "localhost:1099"); 
      env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); 

      // ... specify the JNDI properties specific to the vendor 

      InitialContext jndi = new InitialContext(env); 


      // Look up a JMS connection factory 
      TopicConnectionFactory conFactory = 
      (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory"); 

      // Create a JMS connection 
      TopicConnection connection = 
      conFactory.createTopicConnection(username,password); 





      // Create two JMS session objects 
      TopicSession pubSession = 
      connection.createTopicSession(false, 
              Session.AUTO_ACKNOWLEDGE); 
      TopicSession subSession = 
      connection.createTopicSession(false, 
              Session.AUTO_ACKNOWLEDGE); 

      // Look up a JMS topic 
      Topic chatTopic = (Topic)jndi.lookup(topicName); 

      // Create a JMS publisher and subscriber 
      TopicPublisher publisher1 = 
       pubSession.createPublisher(chatTopic); 
      TopicSubscriber subscriber = 
       subSession.createSubscriber(chatTopic); 

      // Set a JMS message listener 
      subscriber.setMessageListener(this); 

      // Intialize the Chat application 
      set(connection, pubSession, subSession, publisher1, username); 

      // Start the JMS connection; allows messages to be delivered 
      connection.start(); 

     } 
     /* Initialize the instance variables */ 
     public void set(TopicConnection con, TopicSession pubSess, 
         TopicSession subSess, TopicPublisher pub, 
         String username) { 
      this.connection = con; 
      this.pubSession = pubSess; 
      this.subSession = subSess; 
      this.publisher = pub; 
      this.username = username; 
     } 
     /* Receive message from topic subscriber */ 
     public void onMessage(Message message) { 
      try { 
       TextMessage textMessage = (TextMessage) message; 
       String text = textMessage.getText(); 
       System.out.println(text); 
      } catch (JMSException jmse){ jmse.printStackTrace(); } 
     } 
     /* Create and send message using topic publisher */ 
     protected void writeMessage(String text) throws JMSException { 
      TextMessage message = pubSession.createTextMessage(); 
      message.setText(username+" : "+text); 
      publisher.publish(message); 
     } 
     /* Close the JMS connection */ 
     public void close() throws JMSException { 
      connection.close(); 
     } 
     /* Run the Chat client */ 
     public static void main(String [] args){ 
      try{ 
       if (args.length!=3) 
        System.out.println("Topic or username missing"); 

       // args[0]=topicName; args[1]=username; args[2]=password 
       pub chat = new pub(args[0],args[1],args[2]); 

       // Read from command line 
       BufferedReader commandLine = new 
        java.io.BufferedReader(new InputStreamReader(System.in)); 

       // Loop until the word "exit" is typed 
       while(true){ 
        String s = commandLine.readLine(); 
        if (s.equalsIgnoreCase("exit")){ 
         chat.close(); // close down connection 
         System.exit(0);// exit program 
        } else 
         chat.writeMessage(s); 
       } 
      } catch (Exception e){ e.printStackTrace(); } 
     } 
    } 
    i do this ![enter image description here][1] 
    and now i get this eroor 
    ![enter image description here][2] 
    and i dont no what i do not ok i will be happy for help thanks!! 


     [1]: http://i.stack.imgur.com/c04o1.gif 
     [2]: http://i.stack.imgur.com/dxCIU.gif 
+2

歡迎StackOverflow的參數。請閱讀[FAQ](http://stackoverflow.com/faq#questions)瞭解如何提出問題。另外,更改問題標題。 –

+0

不知道它是否在常見問題解答中提到,但我只能閱讀.. *「嗨,我想..」* .. ..停止前。請在每個句子的開頭添加一個大寫字母,以及單詞'I'。這使得它更易於閱讀。你不想讓它難以閱讀,**你會嗎? –

回答

2
java.lang.ArrayIndexOutOfBoundsException: 0 at chat.pub.main(pub.java:105) 

這意味着你啓動的程序沒有給它的參數

然後,在線路105

pub chat = new pub(args[0],args[1],args[2]); 

要訪問args來第0個元素時ARGS是空的。

重新運行程序並根據需要給出參數(3)。

編輯:

爲了在Eclipse中運行時提供的參數:

運行 - >運行配置 - >選擇酒館(既然你已經嘗試在Eclipse中運行它) - >選擇參數 - >下程序參數指定由空格分隔

Pictorial View

+0

嗨,感謝您的幫助我編輯我的messeg後,你的幫助,我添加照片什麼worng後,我這樣做我wuld高興的幫助 – user1346532

+0

很高興成爲幫助:)親切接受答案,如果它幫助你 – mprabhat