I am trying to use the Gmail API and I encountered this run-time exception:
java.lang.NoClassDefFoundError: java/awt/Desktop
It occurs here (on the last line):
InputStream stream = ReadMail.class.getResourceAsStream("/client_secret.json");
Reader clientSecretReader = new InputStreamReader(stream);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(ReadMail.JSON_FACTORY, clientSecretReader);
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
ReadMail.HTTP_TRANSPORT, ReadMail.JSON_FACTORY, clientSecrets, ReadMail.SCOPES)
.setDataStoreFactory(ReadMail.DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
AuthorizationCodeInstalledApp authCodeInstalledApp = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
Credential credential = authCodeInstalledApp.authorize("user");
This code is sitting in the doInBackground()
method of AsyncTask
.
I have checked and Android Studio is configured to use JDK 1.7.
Why does this happen and how can I fix it?
Android does not support all JDK classes, only a subset and java.awt
is not part of it. Therefore java.awt.Desktop
cannot be found. It seems like you're not using the right API at all. You should use Android Quickstart instead.