2012-04-30 4 views
0

JMS로 간단한 채팅 응용 프로그램을 만들고 있지만 코드가 작동하지 않고 그 이유를 모르겠습니다. 이것은 내 코드와 나는 술집을 열고 내가 이것을 쓰고 내가 시작을 클릭하면 이클립스가 나에게이 오류 메시지가 줄 그래서 나는 보스와 함께 사용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

Welcome to StackOverflow. 질문하는 방법에 대해서는 [FAQ] (http://stackoverflow.com/faq#questions)를 읽어보십시오. 또한 질문 제목을 변경하십시오. –

+0

FAQ에 언급되어 있는지 모르겠지만 지금까지만 읽을 수 있습니다 .. * "hi i want .."* .. 멈추기 전에. 모든 문장의 시작 부분에 대문자를 추가하고 단어 'I'를 추가하십시오. 이렇게하면 더 쉽게 읽을 수 있습니다. 당신은 그것을 ** 읽기가 더 어렵게 만들고 싶지 않을 것입니다. –

답변

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

그것은 당신이 그것을 인수를 포기하지 않고 프로그램을 시작 의미를 인수가 비어있을 때

다음 라인 (105)

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

에서 당신은 인수의 0 번째 요소에 액세스.

프로그램을 다시 실행하고 필요한 경우 매개 변수 (3)를 제공하십시오.

편집 : 이클립스에 인수를 제공 실행하기 위해

:

실행 -> 실행 구성 -> 선택 술집 (당신은 이미 이클립스에서 실행 시도 이후) -> 선택 인수 -> 아래 프로그램 인수는 공백으로 구분 된 매개 변수를 지정합니다.

Pictorial View

+0

안녕하세요, 도움 주셔서 감사합니다. 내 messeg를 편집하십시오. 도움을 받으십시오. 사진을 추가하십시오. 도움이 필요하면 도움을 청합니다. – user1346532

+0

도움이된다면 기쁘게 생각합니다. – mprabhat

관련 문제