java.lang.NoClassDefFoundError: com.google.android

2019-08-08 16:19发布

问题:

I am trying to implement push notification for my app. i had set up the project in eclipse added google-play-services.jar in the java buildpath and checked it in order and export.so my app was working absolutely fine when i was doing registration with gcm and was getting the registration id from gcm too.but when i try to build my app from command line i am getting the below error

java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging

i added the google-play-services.jar in the classpath so the code compiled without any issue but at runtime i am getting the above exception.looks like its not able to find the jar at runtime. how can i make the jar available at runtime also please help.

回答1:

A NoClassDefFoundError specifies that a class was available during compile time, but missing during runtime. When you run your application from command line, you need to add the dependencies in your system classpath. Or else you need to package google-play-services.jar and other dependencies along with your code so that the application finds it during runtime.

If you still face issues, refer to this - http://javareferencegv.blogspot.com/2013/10/debugging-javalangnoclassdeffounderror.html



回答2:

Go to configure build path -> order and export and select(check box) the packages(jars).



回答3:

No matter what the build path contains in eclipse, it has nothing to do with an application. All the jars have to be added to the classpath. I would use the following command to start the application after build:

java -cp google.jar;jarA.jar;jarB.jar;jarC.jar -jar application.jar

After the -cp option fill in the semicolon separated list of jars that you use.