您如何调试 Java Applet?

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

How do you debug Java Applets?

javadebuggingapplet

提问by zimbatm

Currently, the only information I have is a one-line error message in the browser's status-bar.

目前,我拥有的唯一信息是浏览器状态栏中的一行错误消息。

Do you know how I could get a stack-trace for example ?

例如,您知道我如何获得堆栈跟踪吗?

采纳答案by Bob Cross

This article is a bit oldbut is still relevant (including a section entitled "How to Debug Applets in Java Plug-in").

这篇文章有点旧,但仍然相关(包括标题为“如何在 Java 插件中调试小程序”的部分)。

Edit: perhaps a better way to get stacktraces is to use the Java plugin console. If you hit "t" in that window, you'll see the following:

编辑:也许获取堆栈跟踪的更好方法是使用Java 插件控制台。如果您在该窗口中点击“t”,您将看到以下内容:

Prints out all the existing thread groups. The first group shown is Group main. ac stands for active count; it is the total number of active threads in a thread group and its child thread groups. agc stands for active group count; it is the number of active child thread groups of a thread group. pri stands for priority; it is the priority of a thread group. Following Group main, other thread groups will be shown as Group , where name is the URL associated with an applet. Individual listings of threads will show the thread name, the thread priority, alive if the thread is alive or destroyed if the thread is in the process of being destroyed, and daemon if the thread is a daemon thread.

打印出所有现有的线程组。显示的第一组是组主。ac 代表活动计数;它是线程组及其子线程组中活动线程的总数。agc 代表活动组数;它是一个线程组的活动子线程组的数量。pri 代表优先级;它是线程组的优先级。在 Group main 之后,其他线程组将显示为 Group ,其中 name 是与小程序关联的 URL。单独的线程列表将显示线程名称、线程优先级、如果线程处于活动状态则为活动状态,如果线程正在被销毁的过程中则显示为活动状态,如果线程是守护线程则显示守护进程。

The other command that I've used most often from that console is the trace level from 0-5:

我从该控制台最常使用的另一个命令是 0-5 的跟踪级别:

This sets the trace-level options as described in the next section, Tracing and Logging.

这将设置跟踪级别选项,如下一节“跟踪和日志记录”中所述。

From that page, you'll see that the levels look like this:

从该页面,您将看到级别如下所示:

  • 0 — off
  • 1 — basic
  • 2 — network, cache, and basic
  • 3 — security, network and basic
  • 4 — extension, security, network and basic
  • 5 — LiveConnect, extension, security, network, temp, and basic
  • 0 — 关闭
  • 1 — 基本
  • 2 — 网络、缓存和基本
  • 3 — 安全、网络和基础
  • 4 — 扩展、安全、网络和基础
  • 5 — LiveConnect、扩展、安全、网络、临时和基本

These tools can all be fairly useful as you're trying to unravel what in the world has gotten into the head of your applets. I know that they've worked for me.

当您试图解开世界上已经进入您的小程序的头脑时,这些工具都非常有用。我知道他们为我工作。

回答by Brian Agnew

The applet viewersupports debug options.

applet浏览器支持的调试选项。

回答by Tom Hawtin - tackline

Stack traces from uncaught exceptions will appear to the console. This can be enabled from the Java Control Panel (Advanced > Java console > Show console) or some browsers have various options or plugins for enabling it.

来自未捕获异常的堆栈跟踪将显示在控制台上。这可以从 Java 控制面板(高级 > Java 控制台 > 显示控制台)启用,或者某些浏览器有各种选项或插件来启用它。

You can attach a debugger to the running PlugIn process.

您可以将调试器附加到正在运行的 PlugIn 进程。

Perhaps the best way is not to debug at all. Write tests. Write code that doesn't couple to unnecessary assumptions - for instance that you are running as an applet. Unfortunately most GUI/applet example code is written very badly.

也许最好的方法是根本不调试。编写测试。编写不会与不必要的假设耦合的代码 - 例如,您作为小程序运行。不幸的是,大多数 GUI/applet 示例代码编写得非常糟糕。

回答by Pool

Uncaught exceptions are sent to the console. You can also use System.out to write your own messages to the console. To view the results you'll need to open the console by right clicking the Java icon in the systray and opening the console (note this is different for Microsoft's VM).

未捕获的异常被发送到控制台。您还可以使用 System.out 将您自己的消息写入控制台。要查看结果,您需要通过右键单击系统托盘中的 Java 图标并打开控制台来打开控制台(请注意,这与 Microsoft 的 VM 不同)。

To debug applets properly, you can setup Eclipse to debug applets. Right click the applet source file and click Debug as Applet. (If you have parameters for the applet you'll need to set this up.) Then you can step through the applet code as you would debug any other Java code.

要正确调试小程序,您可以设置 Eclipse 来调试小程序。右键单击小程序源文件,然后单击作为小程序调试。(如果您有applet 的参数,您需要设置它。)然后您可以单步调试applet 代码,就像调试任何其他Java 代码一样。

回答by Sami Koivu

Aside from the obvious use of the Java console and the applet viewer, starting from Java 6 update 7, you can use the VisualVMthat comes with the JDK (JDK_HOME/bin/visualvm). It allows you to view the stack traces of each thread and even view all object instances.

除了明显使用 Java 控制台和小程序查看器之外,从 Java 6 update 7 开始,您可以使用JDK 附带的VisualVM(JDK_HOME/bin/visualvm)。它允许您查看每个线程的堆栈跟踪,甚至查看所有对象实例。

AppletViewer is very handy, you can do a "Run as / Java Applet" from Eclipse to run, or "Debug As / Java Applet" to debug your applet classes.

AppletViewer 非常好用,您可以从 Eclipse 中执行“Run as/Java Applet”来运行,或者“Debug As/Java Applet”来调试您的小程序类。

However, sometimes to debug some security related stuff the browser plugin environment is just too different from appletviewer. Here's what you can do to effectively debug applets in the browser:

然而,有时为了调试一些与安全相关的东西,浏览器插件环境与 appletviewer 差别太大。以下是在浏览器中有效调试小程序的方法:

1) Obtain debugging info for the binaries

1) 获取二进制文件的调试信息

Backup the .jar files from JRE_HOME/lib

从 JRE_HOME/lib 备份 .jar 文件

(Download and) Install a JDK for the same version as your JRE.

(下载并)安装与 JRE 版本相同的 JDK。

Copy the .jar files from JDK_HOME/jre/lib to JRE_HOME/lib

将 .jar 文件从 JDK_HOME/jre/lib 复制到 JRE_HOME/lib

The files inside the JDK were compiled with the debugging information included (source-code line number information, variable names, etc) and the JRE files don't have this information.

JDK 中的文件编译时包含调试信息(源代码行号信息、变量名等),而 JRE 文件没有这些信息。

Without this you won't be able to meaningfully step into core class code in your debugger.

没有这个,您将无法有意义地进入调试器中的核心类代码。

2) Enable debugging for the Java Plug-in

2) 为 Java Plug-in 启用调试

Go to the Java Control Panel / Java / Java Runtime Settings / View / User / Runtime Parameters

转到 Java 控制面板/Java/Java 运行时设置/查看/用户/运行时参数

And add the options to enable debugging. Something like this:

并添加选项以启用调试。像这样的东西:

-Djava.compiler=NONE -Xnoagent -Xdebug -Xrunjdwp:transport=dt_socket,address=2502,server=y,suspend=n

The interesting options are the port (using 2502 here, you can use pretty much any free port, just write it down for later) and the suspend - if you need to debug the applet startup, classloading, etc, set this to "y". That way when you access an applet page, the browser will appear to freeze as the JVM immediately gets suspended waiting for a debugger to connect.

有趣的选项是端口(这里使用 2502,您几乎可以使用任何空闲端口,只需将其写下来以备后用)和挂起 - 如果您需要调试小程序启动、类加载等,请将其设置为“y” . 这样,当您访问小程序页面时,浏览器似乎会冻结,因为 JVM 会立即暂停,等待调试器连接。

3) Use your favorite IDE to Remotely debug the Java Plug-in

3) 使用您喜欢的 IDE 远程调试 Java Plug-in

In Eclipse, for instance, choose Run / Debug Configurations ... / Remote Java Application

例如,在 Eclipse 中,选择 Run / Debug Configurations ... / Remote Java Application

Click on the "New" button.

单击“新建”按钮。

Make sure connection type is "Socket Attach", choose localhost as the host if your browser is local, and the port you chose earlier (2502 in the example).

确保连接类型为“Socket Attach”,如果您的浏览器是本地的,则选择 localhost 作为主机,以及您之前选择的端口(示例中为 2502)。

You might have to inlude the src.zip in your JDK on the sources tab to have the Java core class sources available.

您可能必须在源选项卡上的 JDK 中包含 src.zip 才能使 Java 核心类源可用。

Save the configuration, and once your browser is running the plug-in (with the JVM suspended or not) run the remote debugger to connect to the plug-in JVM, with a project containing your applet sources open.

保存配置,一旦您的浏览器运行插件(无论是否挂起 JVM),运行远程调试器以连接到插件 JVM,同时打开一个包含小程序源的项目。

回答by ranjeetcao

I faced issue doing remote applet debugging, every time while trying to connect from eclipse, its throws Connection refused error, my jre version was 64 bit and eclipse 32-bit, when I did replace with 32-bit jre, it worked for me. Also if we have install both 32-bit & 64-bit jre versions, IE by default uses 64-bit jre for applets, chrome and FF may use 32-bit jre versions.

我在进行远程小程序调试时遇到了问题,每次尝试从 eclipse 连接时,它都会抛出连接拒绝错误,我的 jre 版本是 64 位,eclipse 32 位,当我确实用 32 位 jre 替换时,它对我有用。此外,如果我们同时安装了 32 位和 64 位 jre 版本,则 IE 默认使用 64 位 jre 用于小程序,chrome 和 FF 可能使用 32 位 jre 版本。

回答by scotchwisky

For me the most important action to get applet debugging in eclipse, is to set in java control panel(tab java) the right binaries to use, that have debug symbols. Only JRE included in jdk have this symbols. So to debug applet in java control panel, tab java, press view button, after find the correct jre under jdk folder, for me for example "C:\Programmi\Java\jdk1.7.0_03\jre", and put check enabled only for this entry. This is for me the clean way to do what Sami Koivu say.

对我来说,在 Eclipse 中进行小程序调试的最重要的操作是在 java 控制面板(选项卡 java)中设置要使用的正确二进制文件,这些二进制文件具有调试符号。只有 jdk 中包含的 JRE 才有这个符号。所以要在java控制面板中调试小程序,标签java,按视图按钮,在jdk文件夹下找到正确的jre后,例如“C:\Programmi\Java\jdk1.7.0_03\jre”,并只启用检查对于这个条目。对我来说,这是按照 Sami Koivu 所说的去做的干净方式。