title: google cloud messag server error
body: <p>I am trying to create a simple chat application for device to device communication  using Google cloud messaging .. when i try to run the server side device to device code
  As listed bellow</p>
  
  <pre><code>`
  
  package com.javapapers.java.gcm;
  
      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.FileReader;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.Map.Entry;
  
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
  
      import com.google.android.gcm.server.Message;
      import com.google.android.gcm.server.MulticastResult;
      import com.google.android.gcm.server.Result;
      import com.google.android.gcm.server.Sender;
  
      @WebServlet("/GCMNotification")
      public class GCMNotification extends HttpServlet {
          private static final long serialVersionUID = 1L;
  
          // Put your Google API Server Key here
          private static final String GOOGLE_SERVER_KEY = "AIzaSyAJuusn84VR9d4jiedBSIy1pE4gQCh2IG0";
          static final String REGISTER_NAME = "name";
          static final String MESSAGE_KEY = "message";
          static final String TO_NAME = "toName";
          static final String REG_ID_STORE = "GCMRegId.txt";
  
          public GCMNotification() {
              super();
          }
  
          protected void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
              doPost(request, response);
  
          }
  
          protected void doPost(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
  
              String action = request.getParameter("action");
  
              if ("shareRegId".equalsIgnoreCase(action)) {
  
                  writeToFile(request.getParameter("name"),
                          request.getParameter("regId"));
                  request.setAttribute("pushStatus",
                          "GCM Name and corresponding RegId Received.");
                  request.getRequestDispatcher("index.jsp")
                          .forward(request, response);
  
              } else if ("sendMessage".equalsIgnoreCase(action)) {
  
                  try {
                      String fromName = request.getParameter(REGISTER_NAME);
                      String toName = request.getParameter(TO_NAME);
                      String userMessage = request.getParameter(MESSAGE_KEY);
                      Sender sender = new Sender(GOOGLE_SERVER_KEY);
                      Message message = new Message.Builder().timeToLive(30)
                              .delayWhileIdle(true).addData(MESSAGE_KEY, userMessage)
                              .addData(REGISTER_NAME, fromName).build();
                      Map&lt;String, String&gt; regIdMap = readFromFile();
                      String regId = regIdMap.get(toName);
                      Result result = sender.send(message, regId, 1);
                      request.setAttribute("pushStatus", result.toString());
                  } catch (IOException ioe) {
                      ioe.printStackTrace();
                      request.setAttribute("pushStatus",
                              "RegId required: " + ioe.toString());
                  } catch (Exception e) {
                      e.printStackTrace();
                      request.setAttribute("pushStatus", e.toString());
                  }
                  request.getRequestDispatcher("index.jsp")
                          .forward(request, response);
              } else if ("multicast".equalsIgnoreCase(action)) {
  
                  try {
                      String fromName = request.getParameter(REGISTER_NAME);
                      String userMessage = request.getParameter(MESSAGE_KEY);
                      Sender sender = new Sender(GOOGLE_SERVER_KEY);
                      Message message = new Message.Builder().timeToLive(30)
                              .delayWhileIdle(true).addData(MESSAGE_KEY, userMessage)
                              .addData(REGISTER_NAME, fromName).build();
                      Map&lt;String, String&gt; regIdMap = readFromFile();
  
                      List&lt;String&gt; regIdList = new ArrayList&lt;String&gt;();
  
                      for (Entry&lt;String, String&gt; entry : regIdMap.entrySet()) {
                          regIdList.add(entry.getValue());
                      }
  
                      MulticastResult multiResult = sender
                              .send(message, regIdList, 1);
                      request.setAttribute("pushStatus", multiResult.toString());
                  } catch (IOException ioe) {
                      ioe.printStackTrace();
                      request.setAttribute("pushStatus",
                              "RegId required: " + ioe.toString());
                  } catch (Exception e) {
                      e.printStackTrace();
                      request.setAttribute("pushStatus", e.toString());
                  }
                  request.getRequestDispatcher("index.jsp")
                          .forward(request, response);
              }
          }
  
          private void writeToFile(String name, String regId) throws IOException {
              Map&lt;String, String&gt; regIdMap = readFromFile();
              regIdMap.put(name, regId);
              PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                      REG_ID_STORE, false)));
              for (Map.Entry&lt;String, String&gt; entry : regIdMap.entrySet()) {
                  out.println(entry.getKey() + "," + entry.getValue());
              }
              out.println(name + "," + regId);
              out.close();
  
          }
  
          private Map&lt;String, String&gt; readFromFile() throws IOException {
              BufferedReader br = new BufferedReader(new FileReader(REG_ID_STORE));
              String regIdLine = "";
              Map&lt;String, String&gt; regIdMap = new HashMap&lt;String, String&gt;();
              while ((regIdLine = br.readLine()) != null) {
                  String[] regArr = regIdLine.split(",");
                  regIdMap.put(regArr[0], regArr[1]);
              }
              br.close();
              return regIdMap;
          }
      }
  `
  </code></pre>
  
  <p>![It shows a error message like
  this <a href="http://i.stack.imgur.com/8YyEJ.png]" rel="nofollow">http://i.stack.imgur.com/8YyEJ.png]</a></p>