如何修复/解决 java.lang.reflect.InvocationTargetException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18285290/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:04:11  来源:igfitidea点击:

How to fix/workaround java.lang.reflect.InvocationTargetException

javawebappletinvocationtargetexception

提问by user2403876

I've got a question about a particularly annoying error that I haven't been able to figure out, much less overcome. Any time I try to run a Java applet (Applet or JApplet) on my website, I get this error as a pop-up:

我有一个关于一个特别烦人的错误的问题,我一直无法弄清楚,更不用说克服了。每当我尝试在我的网站上运行 Java 小程序(Applet 或 JApplet)时,我都会以弹出窗口的形式收到此错误:

 java.lang.reflect.InvocationTargetException

No stack trace, no line number, just the error message. So I've Googled around looking for anyone else's workarounds (or ideally actual fixes) but haven't been able to find much. I've tried several variations of my code (sometimes with a JAR file, sometimes not, sometimes a single class, sometimes not, sometimes in a package using a matching directory structure, sometimes no package, etc.) but can't seem to get past this nasty little son-of-a-bug. :)

没有堆栈跟踪,没有行号,只有错误消息。所以我在谷歌上搜索了其他人的解决方法(或理想情况下的实际修复),但没有找到太多。我已经尝试了我的代码的几种变体(有时使用 JAR 文件,有时不使用,有时使用单个类,有时不使用,有时在使用匹配目录结构的包中,有时没有包等)但似乎无法摆脱这个讨厌的小虫子。:)

For a specific example, here's my most recent attempt; first the Java code:

对于一个具体的例子,这是我最近的尝试;首先是Java代码:

package cmtoolbox;

public class CMToolbox {
    public static void main(String[] args) {
        MainApplet a = new MainApplet();
    }
}

The class it sets up:

它设置的类:

package cmtoolbox;

import javax.swing.JApplet;
import javax.swing.JButton;

public class MainApplet extends JApplet {
    public MainApplet() {
        JApplet main = new JApplet();
        main.setSize(800,600);
        JButton test1 = new JButton();
        test1.setText("Test");
        main.add(test1);
    }
}

My HTML code:

我的 HTML 代码:

<html>
<head>
  <title> Experimenting with Java applets </title>
</head>
<body>
  <p><applet code="CMToolbox.class" width="800" width="600">
    I wish. :)
  </applet></p>
</body>
</html>

I suppose that maybe because the web itself can have so many variables (operating systems, browser types, etc.) there is something internal/system-level causing this... but I do have the JRE and JDK installed on my computer so I don't really get why... Anyway, I'm sure I'm not the first guy to hit this roadblock, but it's got me stumped so I'd appreciate any info that may be available on the subject. Also if you know of any good Java web tutorials for absolute noobs that would be great as well. :)

我想这可能是因为网络本身可以有很多变量(操作系统、浏览器类型等),有一些内部/系统级别的原因导致了这种情况……但我的计算机上确实安装了 JRE 和 JDK,所以我真的不明白为什么......无论如何,我肯定我不是第一个遇到这个障碍的人,但这让我很难过,所以我很感激任何可能有关该主题的信息。此外,如果您知道任何针对绝对新手的优秀 Java 网络教程,那也很棒。:)

采纳答案by Andrew Thompson

InvocationTargetException1is thrown because the HTML is calling (trying to load) something that is notan applet. Change it to:

InvocationTargetException1被抛出是因为 HTML 正在调用(试图加载)不是小程序的东西。将其更改为:

  <p><applet code="MainApplet" width="800" width="600">
  </applet></p>

Also, as mentioned in the answer of Stephen C. Move the stuff from the constructor into the init()method.

另外,正如Stephen C回答中提到的。将构造函数中的内容移到init()方法中。

  1. InvocationTargetExceptionis a checked exception that wraps an exceptionthrown by an invoked method or constructor.
  1. InvocationTargetException是一个已检查的异常,它包装了由调用的方法或构造函数抛出的异常

Ensure the Java Consoleis configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.

确保Java 控制台配置为显示小程序和 JWS 应用程序。如果默认级别没有输出,请将其提高并重试。



While I'm here: Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.

当我在这里时:为什么要编写小程序?如果是由于规格。老师的话,请他们参考为什么 CS 老师应该停止教授 Java 小程序

回答by Stephen C

I suggest that you read the Oracle Applet Development Tutorial. I'm not an expert on applets (understatement!) but you seem to be doing a lot of things differently to how the Tutorial says to do them. For instance, you don't use a mainmethod to launch an applet, and you should be doing the setup in the initmethod not the constructor (see here).

我建议您阅读 Oracle Applet 开发教程。我不是 Applet 方面的专家(轻描淡写!),但您似乎在做许多与教程所说的不同的事情。例如,您不使用main方法来启动小程序,您应该在init方法中而不是在构造函数中进行设置(请参阅此处)。