本文介绍了如何修复java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,大家好,我正在创建 com.google.api.client.json.JsonFactory 的新对象

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

上述问题已解决.现在,我陷入了另一个问题,并且我知道这些问题是由于版本不兼容而引起的.所以任何人都可以告诉我,将YouTube数据API V3与Java包装器集成在一起的依赖性是什么.我正在使用服务帐户进行身份验证.

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.

下面是我的Java代码.

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());
      }

   }
}

新错误控制台.

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依赖::

请访问链接

推荐答案

NoSuchMethodError建议您使用的jar版本不兼容.即

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

您有一个jar,它引用了另一个jar来调用AbstractGoogleJsonClient类的方法,其中com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient类没有所需的方法!

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!

解决方案是找到jar之间的依赖关系(版本依赖关系),并将jar更新为最新/对应版本.

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

为什么不粘贴代码和jar版本?这样我们就可以查看所使用的代码和jars.

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

这篇关于如何修复java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:02
查看更多