0

Hi guys in my code i am making a new object of com.google.api.client.json.JsonFactory as

 final static JsonFactory JSON_FACTORY = new JacksonFactory();

And when i run my program in debug mode, after executing this line . Compiler gives an exception i.e.

    run:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
    at com.google.api.client.json.jackson2.JacksonFactory.<init>(JacksonFactory.java:45)
    at googleauthtest.GoogleAuthTest.<clinit>(GoogleAuthTest.java:25)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 2 more
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

How can I fix this problem ? Thanks in Advance

The above problem has been fixed . Now I am got stuck in another problem and I know these problems are coming due to version incompatibility. So can anyone please tell me that what dependencies are their to integrate YouTube data API V3 with java wrapper. I am using service account for authentication.

Below is my java code..

package googleauthtest;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Authentication {

   final static HttpTransport TRANSPORT = new NetHttpTransport();
   final static JsonFactory JSON_FACTORY = new JacksonFactory();

   com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient h;
   public static void main(String []g) throws Exception
   {
      List<String>scops = new <String>ArrayList();
      scops.add("https://www.googleapis.com/auth/youtube");

      GoogleCredential credential = null;
      try
      {
         credential = new GoogleCredential.Builder()
        .setTransport(TRANSPORT)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(Constants.SERVICE_ACCOUNT_ID)
        .setServiceAccountScopes(scops)
        .setServiceAccountPrivateKeyFromP12File(new File("*****/GoogleAuthTest/src/googleauthtest/key.p12"))
        //.setClientSecrets("74279030240-db333th3oph219blk19kkh540m0gu4f5.apps.googleusercontent.com","")
        //.setClientSecrets(GoogleClientSecrets.load(JSON_FACTORY,new InputStreamReader(java.io.Reader.class.getResourceAsStream("client_secrets.json"))))
        .build();

        credential.refreshToken();
        System.out.println(credential.getAccessToken());
        System.out.println(credential.getTokenServerEncodedUrl());
        System.out.println(credential.getExpiresInSeconds());
        System.out.println(credential.getServiceAccountPrivateKey().toString());
        //credential.getServiceAccountPrivateKey();

        YouTube youtube = new YouTube.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("").build();
      }
      catch(IOException e)
      {
          System.out.println(e.toString());
      }

   }
}

New Error Console..

Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.<init>(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/http/HttpRequestInitializer;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/json/JsonObjectParser;Lcom/google/api/client/googleapis/services/GoogleClientRequestInitializer;Ljava/lang/String;Z)V
    at com.google.api.services.youtube.YouTube.<init>(YouTube.java:135)
    at com.google.api.services.youtube.YouTube$Builder.build(YouTube.java:6446)
    at googleauthtest.Authentication.main(Authentication.java:54)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)

JAR dependencies ::

please visit this link

Sunil Sharma
  • 1,297
  • 3
  • 17
  • 31
  • Have you added the required jar in buildpath – hcl Aug 01 '14 at 12:02
  • Are you using a different Jackson version on top of the one packaged with Google API? Do they package Jackson or should you add it manually? – Mena Aug 01 '14 at 12:03
  • possible duplicate of [How to Solve java.lang.NoClassDefFoundError?](http://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror) – flotothemoon Aug 01 '14 at 12:07
  • @hcl bro, yes I have added corresponding jar file. So that it is not giving any compile time error. But when I run my code it gives same error. – Sunil Sharma Aug 01 '14 at 12:09
  • @Mena I have included this Jackson jar manually . I guess it might possible that they internally use com.fasterxml.jackson.core.JsonFactory and I have added com.google.api.client.json.JsonFactory. So should I change my jar package ??? – Sunil Sharma Aug 01 '14 at 12:14
  • If all required jars are already present, try to move the dependent jars at top of the classpath so that they get loaded before required by the depending jars + your code. – Maas Aug 01 '14 at 12:18
  • @Girish I am using NetBeans 7.3.0 for development. To import necessary jar files, I usually add those file in libraries . So how to maintain the jar dependency in IDE.. ?? – Sunil Sharma Aug 02 '14 at 18:15
  • The root cause for your error is ClassNotFound and you did mention that you added the jar for JSobFactory, in that case move that jar ahead of others in class path. Not sure how to do in NetBeans, but in Eclipse there is a way to change the order of libraries... Search Build path settings – Maas Aug 02 '14 at 18:23
  • @Girish Thanks error gone. Now I am facing another problem . I am not understanding why this is happening. See the error console . Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.googleauthtest.GoogleAuthTest.main(GoogleAuthTest.java:54) Java Result: 1 – Sunil Sharma Aug 02 '14 at 18:56

1 Answers1

0

NoSuchMethodError suggests that you have jar version incompatible. i.e.

you have a jar which refer's other jar for calling a method of class AbstractGoogleJsonClient, wherein the class com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient doesn't have the method required!

The solution is to find the dependencies between the jars (version dependency) and update the jars to latest/corresponding versions.

Why don't you paste your code and jar versions? So that we can look at the code and jars used.

Maas
  • 1,317
  • 9
  • 20
  • I have resolved this error . Now I am stucked over another challenge. Now I am getting Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient. – Sunil Sharma Aug 04 '14 at 11:17
  • I have edited the question with new error and jar image which I have included. Take a look.. – Sunil Sharma Aug 04 '14 at 11:28
  • Thanks. I think this was an issue with earlier versions of Google API (https://code.google.com/p/buzz-java-client/issues/detail?id=18) Try to upgrade your jars to latest Google APIs – Maas Aug 04 '14 at 12:10
  • Thanks buddy . but can you tell me how can I maintain my repository using Maven ?? – Sunil Sharma Aug 04 '14 at 13:08
  • I hope you will get your answers here: https://code.google.com/p/google-oauth-java-client/wiki/Setup – Maas Aug 04 '14 at 13:17