简体   繁体   English

你如何调试Java Applets?

[英]How do you debug Java Applets?

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 ? 你知道如何获得堆栈跟踪吗?

Aside from the obvious use of the Java console and the applet viewer, starting from Java 6 update 7, you can use the VisualVM that comes with the JDK (JDK_HOME/bin/visualvm). 除了明显使用Java控制台和applet查看器之外,从Java 6 update 7开始,您可以使用JDK附带的VisualVM (JDK_HOME / bin / visualvm)。 It allows you to view the stack traces of each thread and even view all object instances. 它允许您查看每个线程的堆栈跟踪,甚至可以查看所有对象实例。

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”来调试您的applet类。

However, sometimes to debug some security related stuff the browser plugin environment is just too different from appletviewer. 但是,有时为了调试一些安全相关的东西,浏览器插件环境与appletviewer太不同了。 Here's what you can do to effectively debug applets in the browser: 以下是在浏览器中有效调试applet的方法:

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". 有趣的选项是端口(在这里使用2502,几乎可以使用任何空闲端口,只需将其写下来以供日后使用)和暂停 - 如果需要调试applet启动,类加载等,请将其设置为“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. 这样,当您访问applet页面时,浏览器似乎会冻结,因为JVM会立即暂停,等待调试器连接。

3) Use your favorite IDE to Remotely debug the Java Plug-in 3)使用您喜欢的IDE远程调试Java插件

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,并打开包含applet源的项目。

This article is a bit old but is still relevant (including a section entitled "How to Debug Applets in Java Plug-in"). 这篇文章有点陈旧但仍然相关(包括标题为“如何在Java插件中调试Applet”的部分)。

Edit: perhaps a better way to get stacktraces is to use the Java plugin console . 编辑:获得堆栈跟踪的更好方法是使用Java插件控制台 If you hit "t" in that window, you'll see the following: 如果您在该窗口中点击“t”,您将看到以下内容:

Prints out all the existing thread groups. 打印出所有现有的线程组。 The first group shown is Group main. 显示的第一组是Group main。 ac stands for active count; ac代表活动计数; it is the total number of active threads in a thread group and its child thread groups. 它是线程组及其子线程组中活动线程的总数。 agc stands for active group count; agc代表活跃的群体计数; it is the number of active child thread groups of a thread group. 它是线程组的活动子线程组的数量。 pri stands for priority; pri代表优先权; 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. 在Group main之后,其他线程组将显示为Group,其中name是与applet关联的URL。 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. 单个线程列表将显示线程名称,线程优先级,如果线程处于活动状态,则为活动状态;如果线程处于被销毁状态,则显示为已销毁;如果线程为守护程序线程,则为守护程序。

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 0 - 关
  • 1 — basic 1 - 基本
  • 2 — network, cache, and basic 2 - 网络,缓存和基本
  • 3 — security, network and basic 3 - 安全,网络和基础
  • 4 — extension, security, network and basic 4 - 扩展,安全,网络和基础
  • 5 — LiveConnect, extension, security, network, temp, and basic 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. 我知道他们为我工作过。

applet viewer支持调试选项。

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. 编写不会与不必要的假设耦合的代码 - 例如,您作为applet运行。 Unfortunately most GUI/applet example code is written very badly. 不幸的是,大多数GUI / applet示例代码编写得非常糟糕。

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. 我遇到了进行远程applet调试的问题,每次尝试从eclipse连接时,它抛出连接拒绝错误,我的jre版本是64位和eclipse 32位,当我用32位jre替换时,它对我有效。 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. 此外,如果我们同时安装32位和64位jre版本,IE默认使用64位jre作为applet,chrome和FF可能使用32位jre版本。

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. 对我来说,在eclipse中获取applet调试最重要的动作是在java控制面板(tab java)中设置要使用的正确二进制文件,即具有调试符号。 Only JRE included in jdk have this symbols. 只有jdk中包含的JRE才有此符号。 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. 所以在java控制面板中调试applet,选项java,按下查看按钮,在jdk文件夹下找到正确的jre之后,对我来说例如“C:\\ Programmi \\ Java \\ jdk1.7.0_03 \\ jre”,并且只启用检查这个条目。 This is for me the clean way to do what Sami Koivu say. 这对我来说是干净的方式来做Sami Koivu所说的。

Uncaught exceptions are sent to the console. 未捕获的异常将发送到控制台。 You can also use System.out to write your own messages to the console. 您还可以使用System.out将自己的消息写入控制台。 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). 要查看结果,您需要通过右键单击系统托盘中的Java图标并打开控制台来打开控制台(请注意,这与Microsoft的VM不同)。

To debug applets properly, you can setup Eclipse to debug applets. 要正确调试applet,可以设置Eclipse来调试applet。 Right click the applet source file and click Debug as Applet. 右键单击applet源文件,然后单击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. (如果您有applet的参数,则需要对其进行设置。)然后您可以像调试任何其他Java代码一样逐步执行applet代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM