Home > Blockchain >  Google API: java.lang.ClassNotFoundException: Didn't find class "sun.misc.Service"
Google API: java.lang.ClassNotFoundException: Didn't find class "sun.misc.Service"

Time:04-09

I've imported all necessary google dependencies for authenticate the user:

def play_services_version = "15.0.1"
implementation 'com.google.api-client:google-api-client:1.33.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.32.1'
implementation 'com.google.apis:google-api-services-drive:v3-rev20211107-1.32.1'
implementation 'com.sun.net.httpserver:http:20070405'
implementation 'com.google.android.gms:play-services-auth:16.0.0'
implementation "com.google.android.gms:play-services-auth:$play_services_version"
implementation "com.google.android.gms:play-services-drive:$play_services_version"
implementation 'com.squareup.okio:okio:1.14.0'

Receiving the user credetials.

/**
 * Creates an authorized Credential object.
 * @param HTTP_TRANSPORT The network HTTP Transport.
 * @return An authorized Credential object.
 * @throws IOException If the credentials.json file cannot be found.
 */
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: "   CREDENTIALS_FILE_PATH);
    }
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));


    //Token Folder
    java.io.File s = new File(TOKENS_DIRECTORY_PATH);
    if(!s.exists()){
        boolean mkdir = s.mkdir();
    }
    File tokenFolder = new File(con.getFilesDir()  
            File.separator   TOKENS_DIRECTORY_PATH);
    if (!tokenFolder.exists()) {
        tokenFolder.mkdirs();
    }

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(tokenFolder))
            .setAccessType("offline")
            .build();

    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();

    Credential credential = new AuthorizationCodeInstalledApp(flow,receiver ).authorize("user");
    //returns an authorized Credential object.
    return credential;
}

Since that Is official google documentation, the code should be on newest version, however executing the code, I receive Failed resolution of: Lsun/misc/Service Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.misc.Service" error.

Is this due to missing dependencies or is that a bug from google?

CodePudding user response:

The first sentence of the documentation that you linked to is: "Complete the steps described in the rest of this page to create a simple Java command-line application that makes requests to the Drive API." (emphasis added) Those instructions are not for Android.

  • Related