Java 调试时如何处理 ClassNotLoadedException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1367730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I deal with a ClassNotLoadedException while debugging?
提问by DanM
So I'm (remotely) debugging a java/jboss application in Eclipse, stepping through line by line. At one point, an array of GridSquare
objects (GridSquare
is a fairly simple, standalone class, contains a few properties and methods) is created by a method call, i.e:
所以我(远程)在 Eclipse 中调试一个 java/jboss 应用程序,逐行逐步调试。在某一时刻,通过方法调用创建了一组GridSquare
对象(GridSquare
是一个相当简单的独立类,包含一些属性和方法),即:
GridSquare[] squares = this.theGrid.getSquares(14, 18, 220, 222);
GridSquare[] squares = this.theGrid.getSquares(14, 18, 220, 222);
...While when I actually execute the code, the squares
array does get populated with GridSquare
objects, I get something odd when stepping through the code and debugging. At a breakpoint on the line immediately following the assignment shown above, if I try to view the squares
array, instead of a value I get this:
...虽然当我实际执行代码时,squares
数组确实填充了GridSquare
对象,但在单步执行代码和调试时我得到了一些奇怪的东西。在上面显示的赋值之后的行上的断点处,如果我尝试查看squares
数组,而不是值,我会得到以下信息:
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
...Anyone know what that's about?
……有人知道那是什么吗?
采纳答案by Yishai
Basically it means the class loader has not loaded the GridSquare[] class. That being said it sounds like a bug in the debugger in some way. The breakpoint association with the code seems to be slightly broken. Either you need to recompile to get the line numbers in sync or some other issue is going on. At that point in the code (after the assignment) it needs to be loaded. Unless getSquares actually returns a subclass (GridSquareSubclass[]) at which point the JVM may not have loaded it because it doesn't need it (yet).
基本上这意味着类加载器尚未加载 GridSquare[] 类。话虽如此,这在某种程度上听起来像是调试器中的一个错误。与代码的断点关联似乎略有中断。要么您需要重新编译以使行号同步,要么正在发生其他一些问题。在代码中的那个点(在赋值之后)它需要被加载。除非 getSquares 实际上返回一个子类 (GridSquareSubclass[]),此时 JVM 可能还没有加载它,因为它(还)不需要它。
回答by studgeek
I have seen this happen in Eclipse when you have a subclass's class variables hiding a parent class's variables. Somehow that confuses Eclipse (and generally is a bad idea anyway :). For example:
当您有子类的类变量隐藏父类的变量时,我已经在 Eclipse 中看到了这种情况。不知何故,这让 Eclipse 感到困惑(而且通常是一个坏主意:)。例如:
class A {
private String a;
}
class B extends A {
public String a;
}
回答by Raghavendra Nanjundappa
I faced the same problem, i just created a public static void main method, created object of same type and Run As java Application, i then removed main method, it now works fine.
我遇到了同样的问题,我刚刚创建了一个 public static void main 方法,创建了相同类型的对象和 Run As java Application,然后我删除了 main 方法,它现在工作正常。
回答by Sanditha Shetty
By initializing GridSquare will solve the problem. squares =new GridSquare();
通过初始化 GridSquare 将解决这个问题。正方形 = 新 GridSquare();
回答by juldeh
//Give a SIZE to the array:
GridSquare[] squares = GridSquare[this.theGrid.size()];
//Fill each element of the array with the object constructor to avoid the null value
for(int i=0; i<this.theGrid.size(); i++){
squares[i] = new GridSquare();
squares[i] = this.theGrid.getSquares(14, 18, 220, 222);
}
回答by James Watkins
I ran into this error because I was running a unit test on code that uses reflection. I had made a special class for testing all the features of the code. Junit apparently uses a separate classloader for test classes (which makes perfect sense) so my code was not able to use reflection on that class. The stack trace I got was extremely generic (java.lang.InstantiationException) but when I checked in debug mode there was more detail on the Exception object itself (org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException) which led me to this conclusion.
我遇到了这个错误,因为我正在对使用反射的代码运行单元测试。我制作了一个特殊的类来测试代码的所有功能。Junit 显然为测试类使用了一个单独的类加载器(这很有意义),所以我的代码无法在该类上使用反射。我得到的堆栈跟踪非常通用 (java.lang.InstantiationException),但是当我在调试模式下检查时,异常对象本身有更多详细信息 (org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException)这让我得出了这个结论。
So I moved the special class to the main classloader (by moving the file from src/test/java to src/main/java) and it worked fine. I don't like this solution but I cannot figure out an alternative. In my case it's not a big deal but I can see how this might be a concern for others.
所以我将特殊类移动到主类加载器(通过将文件从 src/test/java 移动到 src/main/java)并且它工作正常。我不喜欢这个解决方案,但我想不出替代方案。在我的情况下,这没什么大不了的,但我可以看到这可能会引起其他人的关注。