Java奇怪的NoClassDefFoundError

时间:2014-07-22 02:11:53

标签: java oop static jvm

每当我尝试运行此代码块时,我都会收到NoClassDefFoundError

ClassTable.getInstance();

以下是ClassTable的代码

public class ClassTable
{
    private static ClassTable classTable = new ClassTable();
    private Map<String,Object> pricingTable;
    private Date expiry;


    private ClassTable()
    {
        this.expiry = this.getExpiry();
        this.pricingTable = buildPricingTables();
    }

    private Date getExpiry()
    {
        return DateUtils.INSTANCE.getCutOff();
    }

    public static ClassTable getInstance()
    {
      return classTable;
    }
}

我尝试进入方法getInstance();但是,它会立即抛出错误。这是堆栈跟踪

java.lang.NoClassDefFoundError: Could not initialize class helper.ClassTable
    at models.SinglePaymentLoan.getPricingTable(SinglePaymentLoan.java:742)
    at unit.SinglePaymentLoanTest.testPricingTable(SinglePaymentLoanTest.java:135)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:114)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:47)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:58)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at play.test.TestEngine.run(TestEngine.java:112)
    at controllers.TestRunner$1.doJobWithResult(TestRunner.java:71)
    at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
    at play.jobs.Job.call(Job.java:146)
    at play.jobs.Job$1.call(Job.java:66)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

1 个答案:

答案 0 :(得分:2)

请注意,它声明无法初始化类。这表明类的静态初始化存在问题。

您有一个静态变量classTable尝试创建一个新的ClassTable实例。

private static ClassTable classTable = new ClassTable();
private Map<String,Object> pricingTable;
private Date expiry;

private ClassTable()
{
    this.expiry = this.getExpiry();
    this.pricingTable = buildPricingTables();
}

所以现在创建ClassTable实例时出现的任何错误都会导致类初始化失败。你在构造函数中做了两件事,每件事都可能失败。

您致电getExpiry,我们会看到转弯并致电DateUtils.INSTANCE.getCutOff()。您尚未提供此代码,因此不清楚这是做什么的。

最后,您致电buildPricingTables()。同样,还没有为此方法提供代码。

所以你的两个人很可能是罪魁祸首DateUtils.INSTANCE.getCutOff()buildPricingTables()