执行字节码(java)时忽略异常?

发布于 2024-12-02 22:06:47 字数 333 浏览 1 评论 0原文

我有一个大型程序,是用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 技术交流群。

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

发布评论

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

评论(3

云醉月微眠 2024-12-09 22:06:47

除非代码实际上不需要该类,否则无法忽略 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.

祁梦 2024-12-09 22:06:47

如果您遇到 ClassNotFoundException 问题,那么您始终可以使用 try { ... } catch (...) { ... }

如果您收到 ClassNotFoundErrors,那么这不是反射的可本地化问题,而是初始化所需代码的失败。您应该尝试删除不需要的依赖项,但您确实不应该使用尚未正确初始化的类。

如果确实有必要,您始终可以使用 custom ClassLoader 为任何无法使用系统类加载器解析的名称生成虚假的空类,并使用它来加载主类。这将在某种程度上复制您的 IDE 所做的事情,尽管您的 IDE 可能会采取额外的步骤来确保部分定义良好的类具有正确的接口,即使某些方法因为其主体无法编译而被删除。

If you are having trouble with ClassNotFoundExceptions then you can always localize the problem and catch and log using try { ... } catch (...) { ... }.

If you are instead getting ClassNotFoundErrors 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.

我们的影子 2024-12-09 22:06:47
  1. 您只能忽略编译器警告。你不能忽视错误。
  2. IntelliJ 显示的错误来自同一个编译器。
  3. ClassNotFoundException 表示您的代码无法在运行时动态加载类。
  4. 这可能意味着您的类路径中缺少所需的依赖项(jar)。尝试查阅您的代码文档并确保您已解决所有运行时依赖项。还要确保依赖的 jar 位于类路径中,否则运行时将无法找到它们。
  1. You can only ignore compiler warnings. You cannot ignore errors.
  2. The errors that IntelliJ shows are coming from the same compiler.
  3. ClassNotFoundException would indicate that your code failed to dynamically load a class at runtime.
  4. This could mean that a required dependency (jar) is missing from your classpath. Try to consult your code documentation and make sure you've resolved all runtime dependencies. Also make sure that the dependent jars are in the classpath otherwise the runtime won't be able to find them.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文