执行字节码(java)时忽略异常?
我有一个大型程序,是用java修改的。我使用的是intelliJ idea(社区版)IDE进行编译。当我运行该程序时,它会启动 GUI,然后继续执行我想要的所有操作,几乎没有问题(其中与异常无关)。但代码总是会生成类未找到异常(即使是原始未修改的代码,一旦从 .jar 文件中提取它,也会出现这种情况。尽管存在这些错误,但它在 IDE 中完美执行,同时仍然注意到错误,但它们不会出现然而,当我从虚拟机(使用java文件名)执行它们时,通常被忽略的异常会阻止程序的最终执行。显示,但是 IDE 有一个选项可以传递给 java - 例如 java -ignoreerrors 文件名)?
忽略它们!我怎样才能让虚拟机忽略错误并执行程序(是否
I have a large program, that i modified in java. I used the intelliJ idea (community edition) IDE for compiling. When i go to run the program, it starts up the GUI and then proceeds to do everthing i want from it, with very few problems (of which are unrelated to the exceptions). But the code always generates class not found exceptions (even the original unmodified code does this once you extract it from the .jar file. Despite these errors, it executes within the IDE perfectly, while still noting the errors, but they don't appear to have an effect on the program. However, when i execute them from within the virtual machine (with java filename) the exceptions which are usually ignored prevent the ultimate execution of the program. The errors are exactly the same as the ones that the iDE shows, but the IDE ignores them! How could i get a virtual machine to ignore the errors and execute the program (is there an option to pass to java - for example java -ignoreerrors filename).
Is this possible, or will i have to alter the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非代码实际上不需要该类,否则无法忽略 ClassNotFoundExceptions。某些框架通过尝试加载类来发现某些功能是否可用来实现这一点。但是,如果 CNFE 阻止您的应用程序运行,您只需修复它即可。如果您显示一些堆栈跟踪,有人可能能够引导您走向正确的方向。
There's no way to ignore ClassNotFoundExceptions unless that class isn't actually needed by the code. Some frameworks do that by trying to load a class to discover whether some feature is available. However, if a CNFE is preventing your app from running, you'll just have to fix it. If you show some stack traces, someone might be able to steer you in the right direction.
如果您遇到
ClassNotFoundException
问题,那么您始终可以使用try { ... } catch (...) { ... }
。如果您收到
ClassNotFoundError
s,那么这不是反射的可本地化问题,而是初始化所需代码的失败。您应该尝试删除不需要的依赖项,但您确实不应该使用尚未正确初始化的类。如果确实有必要,您始终可以使用 custom
ClassLoader
为任何无法使用系统类加载器解析的名称生成虚假的空类,并使用它来加载主类。这将在某种程度上复制您的 IDE 所做的事情,尽管您的 IDE 可能会采取额外的步骤来确保部分定义良好的类具有正确的接口,即使某些方法因为其主体无法编译而被删除。If you are having trouble with
ClassNotFoundException
s then you can always localize the problem and catch and log usingtry { ... } catch (...) { ... }
.If you are instead getting
ClassNotFoundError
s then it's not a localizable problem with reflection, but a failure to initialize code that's needed. You should try to prune unneeded dependencies but you really shouldn't use classes that haven't initialized properly.If you absolutely have to, you can always load your program using a custom
ClassLoader
that generates bogus empty classes for any name that is not resolvable using the system classloader and use that to load your main class. That will replicate, to some degree, what your IDE is doing, though your IDE probably goes the extra step to make sure that partially well-defined classes have the right interface even if some methods are stubbed out because their bodies don't compile.ClassNotFoundException
表示您的代码无法在运行时动态加载类。ClassNotFoundException
would indicate that your code failed to dynamically load a class at runtime.