gpt4 book ai didi

java - 奇怪的 ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 09:55:56 24 4
gpt4 key购买 nike

当我在 eclipse 中调试时,这行代码工作正常。

this.primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
InputManager.Instance().addEvent(keyEvent);
}
});

当我使用自己的代码(而不是脚本)将其导出到 .jar 时,它会抛出一个相当长的异常。

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unk
nown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(
Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: test/Game$1
at test.Game.start(Game.java:62)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Sourc
e)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown S
ource)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
... 1 more
Caused by: java.lang.ClassNotFoundException: test.Game$1
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more
Exception running application test.Game

但是,当我以相同的方式导出它,但注释掉处理程序时,它工作得很好。我尝试过 root.addEventHandler、root/primaryStage.setOnKeyPressed,单独声明处理程序,..

你能找出原因吗?

最佳答案

export it into .jar using my own code

您的“自己的代码”做得不正确,因为它没有在 .jar 文件中包含 test/Game$1.class 文件。

您的类在 test 包中称为 Game。当类使用匿名类时,必须为这些类指定一个名称,以便创建 .class 文件(内部也需要该名称)。此类文件的名称可以是任何名称,但当前方案是在定义类之后命名,在$符号后添加序列号,因此第一个匿名类称为 Game$1.class,第二个称为 Game$2.class,依此类推。

这就是为什么,当您编译 Game.java 时,您最终会得到 2 个文件:Game.classGame$1.class这两个文件都必须添加到 .jar 文件中。

如果可能的话,您不应该依赖此命名约定,因为它将来可能会发生变化,并且其他编译器可能会采取不同的做法。

关于java - 奇怪的 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37220402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com