How to fix java.lang.NoClassDefFoundError

泄露秘密 提交于 2019-12-12 04:47:02

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25079795/how-to-fix-java-lang-noclassdeffounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!