Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get class com.google.common.base.Precondition for Java gDrive api?

I'm trying to get a Java interface going with Google Drive API by following the quickstart tutorial, but every time I run my program it throws "NoClassDefFoundError: com/google/common/base/Preconditions." This has also happened when I tried implementing the Google CustomSearchEngine API.

I have referenced all API classes with and without sources and javadocs attached. I have also attempted using findJAR dot com in attempt to obtain Preconditions with little success.

Here are the lines of the code I used that the error references. I have also included the the lines which define the variables used within the error lines in comments above the referenced code.

Line 61:

/*
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

InputStream in = new FileInputStream(clientSecretFilePath);
*/

GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

Line 88:

/*
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
*/

Credential credential = getCredentials(HTTP_TRANSPORT);

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
    at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
    at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:80)
    at com.google.api.client.json.JsonFactory.fromReader(JsonFactory.java:236)
    at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:192)
    at drive.GDrive.getCredentials(GDrive.java:61)
    at drive.GDrive.main(GDrive.java:88)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

It appears to behave as if a class is missing, but I have referenced all API libraries from Google Drive's API in the build config. I have even tried getting the missing class from findjar dot com with little additional progress.

like image 430
Percy Jackson Avatar asked May 25 '19 13:05

Percy Jackson


2 Answers

Remove existing dependencies and add dependencies from below links:

google-api-client-1.23.0

google-oauth-client-jetty-1.23.0

google-api-services-drive-v3-rev110-1.23.0

Happy Coding :-)

like image 113
Shailesh Bhokare Avatar answered Oct 05 '22 16:10

Shailesh Bhokare


I had the same problem while implementing PayU SDK for Android. Solution was to add guava dependencies for android, not the jre:

  implementation("com.google.guava:guava:28.1-android")

For more information on it, see the repository or check the Android guava wiki, preferably Preconditions

I also advise to take a look into Proguard rules for Guava to exclude all the things you don't use in the project from builds.

like image 28
Kuba Pawłowski Avatar answered Oct 05 '22 15:10

Kuba Pawłowski