调试时如何处理 ClassNotLoadedException?

发布于 2024-08-04 02:34:10 字数 492 浏览 10 评论 0原文

因此,我正在 Eclipse 中(远程)调试 java/jboss 应用程序,逐行进行调试。在某一时刻,通过方法调用创建一个 GridSquare 对象数组(GridSquare 是一个相当简单的独立类,包含一些属性和方法),即:

< code>GridSquare[] squares = this.theGrid.getSquares(14, 18, 220, 222);

...当我实际执行代码时,squares 数组确实被填充对于 GridSquare 对象,我在单步执行代码和调试时遇到了一些奇怪的情况。在上面所示的赋值之后的行的断点处,如果我尝试查看 squares 数组,而不是值,我会得到以下内容:

org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException:检索数组的组件类型时发生类型未加载。

...有人知道这是怎么回事吗?

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:

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遥远的绿洲 2024-08-11 02:34:11

我遇到此错误是因为我正在对使用反射的代码运行单元测试。我创建了一个特殊的类来测试代码的所有功能。 Junit 显然对测试类使用单独的类加载器(这非常有意义),因此我的代码无法在该类上使用反射。我得到的堆栈跟踪非常通用(java.lang.InstantiationException),但是当我在调试模式下检查时,有关于 Exception 对象本身的更多详细信息(org.eclipse.debug.core.DebugException:com.sun.jdi.ClassNotLoadedException)这让我得出了这个结论。

因此,我将特殊类移至主类加载器(通过将文件从 src/test/java 移至 src/main/java),并且工作正常。我不喜欢这个解决方案,但我找不到替代方案。就我而言,这不是什么大问题,但我可以看到这可能会引起其他人的担忧。

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.

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.

青萝楚歌 2024-08-11 02:34:11

我在 Eclipse 中看到过这种情况,当子类的类变量隐藏父类的变量时。不知何故,这让 Eclipse 感到困惑(而且通常来说这是一个坏主意:)。例如:

class A {
   private String a;
}

class B extends A {
   public String a;
}

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:

class A {
   private String a;
}

class B extends A {
   public String a;
}
陌上青苔 2024-08-11 02:34:11
//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);
}
//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);
}
婴鹅 2024-08-11 02:34:11

通过初始化GridSquare就可以解决问题。
方块=new GridSquare();

By initializing GridSquare will solve the problem.
squares =new GridSquare();

孤君无依 2024-08-11 02:34:11

我遇到了同样的问题,我刚刚创建了一个 public static void main 方法,创建了相同类型的对象并作为 java 应用程序运行,然后删除了 main 方法,它现在工作正常。

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.

活泼老夫 2024-08-11 02:34:10

基本上这意味着类加载器尚未加载 GridSquare[] 类。话虽这么说,在某种程度上这听起来像是调试器中的错误。与代码的断点关联似乎略有损坏。您需要重新编译以使行号同步,否则会出现其他问题。此时代码中(分配之后)需要加载它。除非 getSquares 实际上返回一个子类 (GridSquareSubclass[]),否则 JVM 可能还没有加载它,因为它还不需要它。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文