-1

This is the server side code for GCM notification, i have build this successfully on ubuntu using command line by adding gcm-server.jar, but at run time it crashes with exception. I have added the jar in the classpath.

  Exception at run time :  
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/android  /gcm/server/Message$Builder  
    at geo.sendNotification.sendNow(sendNotification.java:30)  
    at geo.geosense.main(geosense.java:27)  
Caused by: java.lang.ClassNotFoundException:   com.google.android.gcm.server.Message$Builder  
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)  
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)  
..

.

import org.json.simple.*;  
import java.util.*;  
import com.google.android.gcm.server.Message.Builder;  
import com.google.android.gcm.server.Message;  
import com.google.android.gcm.server.Sender;  
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.MulticastResult;  
public class sendNotification {  
Sender sender = null;  
public void sendNotification ()  // function to send message   
    {  
Sender sender = new Sender("XYZ"); // api key      
    }  
    public void sendNow(String deviceID, String msg, String param)  
    {  

    Message message = new Message.Builder()  
                    .addData("message", msg)  
                    .addData("parameter one", param)  
                    .build();  
    try{      
    Result result = sender.send(message, deviceID, 1);  
    }  
    catch ( Exception e )  
    {  
        e.printStackTrace();  
    }      
    }}  

____________________________________________________________________________

1 Answers1

0

NoClassDefFoundError means that the application is unable to find the location of the file inside the Application's folder. It is advised to keep all the library files and jar files in libs folder of the project.

Make sure that you have put the gcm.jar file inside the libs folder and not lib folder.

To do that go to project properties > java build path > order and export then give a tick on the jar file that you have added.

Here's a related SO ticket which discuss about NoClassDefFoundError: java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging

Community
  • 1
  • 1
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
  • I am building the application on linux using command line. All jars are in the lib folder. I have also renamed to libs but no access. The application builds successfully but crahes with NoClassDefFoundError exception – shubham singh Aug 19 '16 at 09:15