【问题标题】:NoClassDefFoundError: com/google/common/reflect/TypeTokenNoClassDefFoundError:com/google/common/reflect/TypeToken
【发布时间】:2015-08-11 20:18:39
【问题描述】:

我一直在使用this api。该 API 是具有 maven 依赖关系的 Mailchimp API 的 Java 包装器。

<dependency>
    <groupId>com.ecwid</groupId>
    <artifactId>ecwid-mailchimp</artifactId>
    <version>2.0.1.0</version>
</dependency>

到目前为止,我在使用他们的 API 时没有遇到任何问题。但现在我看到了这个奇怪的异常:

Exception in thread "Timer-2" java.lang.NoClassDefFoundError: com/google/common/reflect/TypeToken
at com.ziprealty.subscription.MailChimpNewsSubscriptionProcessor.updateAllUnSubscribedEmails(MailChimpNewsSubscriptionProcessor.java:84)
at com.ziprealty.job.MailChimpSubscriptionProcessor.processTask(MailChimpSubscriptionProcessor.java:29)
at com.ziprealty.job.JobBase.run(JobBase.java:96)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.ClassNotFoundException: com.google.common.reflect.TypeToken
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
... 5 more

这里是 updateAllUnSubscribedEmails 的代码

public void updateAllUnSubscribedEmails( Date lastRunDate, String brandCode,Logger logger){


    logger.log(Level.SEVERE, "Entering mail chimp subscription processor in boardwalk for :" + brandCode);
    logger.log(Level.SEVERE, "Last run date is :" + lastRunDate);
    System.out.println("Entering mail chimp subscription processor in boardwalk for :" + brandCode);

    try {

        MailChimpSubscriptionDAO subscriptionDAO = MailChimpSubscriptionDAO.INSTANCE;
        MailChimpSubscription  subscription= subscriptionDAO.getMailChimpSubscriptionByBrandCode(brandCode);

        logger.log(Level.SEVERE,"Subscription object is :"+ subscription);

        **ListMembersMethod listMembersMethod= new ListMembersMethod();**

        logger.log(Level.SEVERE,"listMembersMethod object is :"+ listMembersMethod);
        listMembersMethod.status= MemberStatus.unsubscribed;
        logger.log(Level.SEVERE,"listMembersMethod.status object is :"+ listMembersMethod.status);
        listMembersMethod.apikey=mailChimpApiKey;
        logger.log(Level.SEVERE,"listMembersMethod.apikey object is :"+ listMembersMethod.apikey);
        listMembersMethod.id=subscription.getEmailListId();
        logger.log(Level.SEVERE,"listMembersMethod.id object is :"+ listMembersMethod.id);

        listMembersMethod.since= lastRunDate;
        .
    .
    .
    .

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        logger.log(Level.SEVERE, e.getMessage());
    } catch (MailChimpException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        logger.log(Level.SEVERE, e.getMessage());
    }
    catch (Exception e){
        e.printStackTrace();
        System.out.println(e.getMessage());
        logger.log(Level.SEVERE, e.getMessage());
    }

    finally {

        logger.log(Level.SEVERE,"Finally block ...Try catch block ended");
    }

    logger.log(Level.SEVERE,"After finally Try catch block  without exception  ");


}

代码在这一行停止工作: ListMembersMethod listMembersMethod= new ListMembersMethod(); 它甚至根本没有进入 Exceptions 块。只到 finally 块。

这是 Intelij IDEA 为 ListMembersMethod 类生成的代码:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ecwid.mailchimp.method.v1_3.list;

import com.ecwid.mailchimp.MailChimpAPIVersion;
import com.ecwid.mailchimp.MailChimpMethod.Method;
import com.ecwid.mailchimp.MailChimpObject.Field;
import com.ecwid.mailchimp.method.v1_3.list.HasListIdMethod;
import com.ecwid.mailchimp.method.v1_3.list.ListMembersResult;
import com.ecwid.mailchimp.method.v1_3.list.MemberStatus;
import java.util.Date;

@Method(
    name = "listMembers",
    version = MailChimpAPIVersion.v1_3
)
public class ListMembersMethod extends HasListIdMethod<ListMembersResult> {
    @Field
    public MemberStatus status;
    @Field
    public Date since;
    @Field
    public Integer start;
    @Field
    public Integer limit;

    public ListMembersMethod() {
    }
}


Intelij Idea has also generated the following code for hasHasListIdMethod :

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ecwid.mailchimp.method.v1_3.list;

import com.ecwid.mailchimp.MailChimpMethod;
import com.ecwid.mailchimp.MailChimpObject.Field;

public abstract class HasListIdMethod<R> extends MailChimpMethod<R> {
    @Field
    public String id;

    public HasListIdMethod() {
    }
}

MailChimpMethod 具有以下代码,其中包含 TypeToken

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ecwid.mailchimp;

import com.ecwid.mailchimp.MailChimpAPIVersion;
import com.ecwid.mailchimp.MailChimpObject;
import com.ecwid.mailchimp.MailChimpObject.Field;
import com.google.common.reflect.TypeToken;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class MailChimpMethod<R> extends MailChimpObject {
    private final TypeToken<R> resultTypeToken = new TypeToken(this.getClass()) {
    };
    @Field
    public String apikey;

    public MailChimpMethod() {
    }

    public final MailChimpMethod.Method getMetaInfo() {
        for(Class c = this.getClass(); c != null; c = c.getSuperclass()) {
            MailChimpMethod.Method a = (MailChimpMethod.Method)c.getAnnotation(MailChimpMethod.Method.class);
            if(a != null) {
                return a;
            }
        }

        throw new IllegalArgumentException("Neither " + this.getClass() + " nor its superclasses are annotated with " + MailChimpMethod.Method.class);
    }

    public final Type getResultType() {
        Type type = this.resultTypeToken.getType();
        if(!(type instanceof Class) && !(type instanceof ParameterizedType) && !(type instanceof GenericArrayType)) {
            throw new IllegalArgumentException("Cannot resolve result type: " + this.resultTypeToken);
        } else {
            return type;
        }
    }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public @interface Method {
        MailChimpAPIVersion version();

        String name();
    }
}

非常感谢您在这方面的帮助。无法弄清楚修复方法是什么。

【问题讨论】:

    标签: java intellij-idea google-api noclassdeffounderror typetoken


    【解决方案1】:

    MailChimp API Wrapper 2.0.1.0 依赖于 Guava 16.0.1(参见 mvnrepository.com/artifact/com.ecwid/ecwid-mailchimp/2.0.1.0)。 com.google.common.reflect.TypeToken 类是 Guava 16.0.1 的一部分(参见 central.maven.org/maven2/com/google/guava/guava/16.0.1/guava-16.0.1.jar)。

    您是否使用 Maven 来构建您的项目,如果是这种情况,您是否可以毫无问题地编译/测试您的代码?是否为该项目下载了 Guava?运行mvn dependency:tree -Dverbose 时看到了什么?

    【讨论】:

    • 感谢您的回复@Freek。我发现了这个问题。请看答案。
    • 这应该是公认的答案。一些 lib 引入了旧版本的 Guava。排除它。谢谢,弗里克!
    【解决方案2】:

    我花了一些时间,但我发现了问题。我正在开发两个依赖 MailChimp API 的独立项目。但是,我没有对其中一个项目的 Maven 依赖,这导致了问题。

    因此包含了对这两个项目的 maven 依赖,它就像一个魅力!

    <dependency>
        <groupId>com.ecwid</groupId>
        <artifactId>ecwid-mailchimp</artifactId>
        <version>2.0.1.0</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2018-07-10
      • 2014-12-04
      • 2014-09-17
      • 1970-01-01
      • 2017-12-08
      • 2020-10-05
      相关资源
      最近更新 更多