简体   繁体   English

NoClassDefFoundError

[英]NoClassDefFoundError

I have an issue where NoClasDefFoundError is being thrown. 我有一个引发NoClasDefFoundError的问题。 It puzzles me since I am using interfaces, and no class definition should be available. 因为使用接口,这让我感到困惑,并且没有可用的类定义。 I have read through some posts which point to Classpath, but I don't believe that to be the issue here (although I may be wrong). 我已经阅读了一些指向Classpath的帖子,但是我不认为这是这里的问题(尽管我可能错了)。 I am using NetBeans 6.9.1 IDE. 我正在使用NetBeans 6.9.1 IDE。

I have created a sample setup to reproduce the issue. 我创建了一个示例设置来重现该问题。 Four projects: Interfaces, Objects, Locator and Consumer. 四个项目:接口,对象,定位器和使用者。 Below you will find the implementations. 在下面,您将找到实现。

At runtime consumer coplains about missing SomeObject implementation, which it should not be aware of since it is accepting interface. 消费者在运行时会抱怨缺少SomeObject实现,由于它正在接受接口,因此不应意识到。

Exception in thread "main" java.lang.NoClassDefFoundError: objects/SomeObject 线程“主”中的异常java.lang.NoClassDefFoundError:objects / SomeObject

What am I missing? 我想念什么?

package interfaces;
public interface ISomeInterface { }

package objects;
import interfaces.ISomeInterface;
public class SomeObject implements ISomeInterface{ }

package locator;
import interfaces.ISomeInterface;
import objects.SomeObject;
public class Locator { public static ISomeInterface LocateImplementation() { return new SomeObject(); }}

package consumer;
import interfaces.ISomeInterface;
import locator.Locator;
public class Main { public static void main(String[] args) { ISomeInterface object = Locator.LocateImplementation(); }}

You can get a NoClassDefFoundError exception with interfaces just as you can with classes. 您可以像使用类一样使用接口获得NoClassDefFoundError异常。 Consider the "Class" in the name of the exception to be the .class file that is generated from compiling a class or interface, not a Java class. 认为异常名称中的“类”是通过编译类或接口而不是Java类而生成的.class文件。

This is saying that the class/interface objects.SomeObject isn't visible on your classpath. 就是说类/接口对象。SomeObject在您的类路径上不可见。 Check the location of that .class file and ensure that it's on your classpath - if you're positive it's there, give us some screen shots or something that might help to debug the problem. 检查该.class文件的位置,并确保它在您的类路径中-如果您肯定它在其中,请给我们提供一些屏幕截图或可能有助于调试问题的内容。

Think of NoClassDefFoundError as a runtime linkage problem. 将NoClassDefFoundError视为运行时链接问题。 JRE loaded one class (or an interface) and it references another class (or an interface), but that referenced class isn't found. JRE加载了一个类(或一个接口),并且引用了另一个类(或一个接口),但是找不到该引用的类。

The only way this can happen if you have packaging/classpath issues such that your runtime environment doesn't reflect how things are at build time. 如果存在打包/类路径问题,以至于运行时环境无法反映构建时的情况,则可能发生这种情况。

If you are launching this from IDE, make sure that you aren't ignoring any errors and launching anyway. 如果要从IDE启动此程序,请确保您不会忽略任何错误并仍要启动。 Some classes will not be generated that way. 某些类不会以这种方式生成。

Usually I run into these problems not when a class is missing, but when there is an error in the static initializers. 通常,不是在缺少类时,而是在静态初始化器中出现错误时,我都会遇到这些问题。

Try running your code in a debugger, and set the exception breakpoint to break when any exception is thrown, whether caught or not. 尝试在调试器中运行代码,并将异常断点设置为在引发任何异常(无论是否捕获)时中断。 I bet you have an uncaught exception in the static initializer for some reason. 我敢打赌,由于某种原因,您在静态初始值设定项中有未捕获的异常。

Any Interface must be declared inside class 任何接口都必须在类内部声明

public class Calbacks {
    public interface IBaseFragmentInterface {
        void NotifyMainActivity();
    }
}

In the locateImplementation() method you are returning "new SomeObject()", JVM needs to have its definition when called. 在locateImplementation()方法中,您将返回“ new SomeObject()”,JVM在调用时需要具有其定义。 I think it is missing. 我认为它不见了。

You should check if your SomeObject class is in class path because - 您应该检查SomeObject类是否在类路径中,因为-

Well the JVM will be running the below code - 好吧,JVM将运行以下代码-

ISomeInterface object = Locator.LocateImplementation();

and when it does that it will call Locator.LocateImplementation(). 并在执行该操作时将调用Locator.LocateImplementation()。 This code internally tries to instantiate your SomeObject class which it does not find in the classpath. 此代码在内部尝试实例化您的SomeObject类,该类在类路径中找不到。

So your below understanding 所以你下面的理解

It puzzles me since I am using interfaces, and no class definition should be available. 因为使用接口,这让我感到困惑,并且没有可用的类定义。

Is not really valid. 不是真的有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM