在浏览器中调试 java applet - 在 Eclipse 中有效,但在浏览器中无效
我创建了一个小程序,它打开一个 JFileChooser
来通过单击 JButton
选择一个文件。当我在 Eclipse 中运行它时,它工作得很好。当我将它嵌入到带有 applet 标记的 HTML 页面中时,单击按钮时没有任何反应。
任何有关为什么 JFileChooser
未在浏览器中打开的建议将不胜感激,但我的主要问题是如何调试它?我一直无法找到Google 上有关如何将 Java 控制台添加到 Firefox 3.6 或 Chrome 的任何信息。有没有办法获取有关 JFileChooser 未打开原因的某种信息?
调试在下面的评论中回答
所以控制台说存在访问被拒绝的异常,我猜测这是因为我还没有“签名”小程序。就签署小程序而言,开发流程应该是怎样的?在我可以在浏览器中测试它之前,我是否必须使用有效 CA 颁发的证书对其进行签名,或者在测试时您可以做一些简单的事情吗?
这是我的代码:
package com.putapplet;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class PutToS3Applet extends JApplet {
private static long maxFileSizeInBytes = 1048576;
private static String listenerUrl = "xxx";
private String listenerQueryString;
private String signedPutUrl;
private JProgressBar progressBar;
private JButton button;
private JLabel messageField;
public void init() {
setMaxFilesize(1);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
button = new JButton("Choose File...");
button.addActionListener(new ButtonListener());
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
messageField = new JLabel();
//messageField.setPreferredSize(new Dimension(300, 20));
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
JPanel topPanel = new JPanel();
topPanel.setLayout(layout);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(layout);
topPanel.add(button);
topPanel.add(progressBar);
bottomPanel.add(messageField);
setLayout(new GridLayout(2,1));
add(topPanel);
add(bottomPanel);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int showOpenDialog = fileChooser.showDialog(null, "Upload File");
if (showOpenDialog != JFileChooser.APPROVE_OPTION) return;
final File fileToUpload = fileChooser.getSelectedFile();
if (fileToUpload.length() > PutToS3Applet.maxFileSizeInBytes) {
messageField.setText("Your file must be smaller than " + getHumanReadableMaxFilesize());
return;
}
listenerQueryString = "query[filename]=" + fileToUpload.getName();
//get signed PUT url for s3
try {
URL listener = new URL(listenerUrl + listenerQueryString);
BufferedReader in = new BufferedReader(new InputStreamReader(listener.openStream()));
signedPutUrl = in.readLine().replace("http://", "https://");
messageField.setText(signedPutUrl);
} catch (Exception e) {
messageField.setText("Oops, there was a problem connecting to the server. Please try again.");
e.printStackTrace();
}
}
}
private String getHumanReadableMaxFilesize() {
return Long.toString((PutToS3Applet.maxFileSizeInBytes / 1024 / 1024)) + "MB";
}
private void setMaxFilesize(int sizeInMegaBytes) {
PutToS3Applet.maxFileSizeInBytes = PutToS3Applet.maxFileSizeInBytes * sizeInMegaBytes;
}
}
这是例外:
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1285)
at java.lang.System.getProperty(System.java:650)
at javax.swing.filechooser.FileSystemView.getHomeDirectory(FileSystemView.java:393)
at javax.swing.plaf.metal.MetalFileChooserUI.installComponents(MetalFileChooserUI.java:253)
at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:136)
at javax.swing.plaf.metal.MetalFileChooserUI.installUI(MetalFileChooserUI.java:126)
at javax.swing.JComponent.setUI(JComponent.java:662)
at javax.swing.JFileChooser.updateUI(JFileChooser.java:1763)
at javax.swing.JFileChooser.setup(JFileChooser.java:360)
at javax.swing.JFileChooser.<init>(JFileChooser.java:333)
at javax.swing.JFileChooser.<init>(JFileChooser.java:286)
at com.putapplet.PutToS3Applet$ButtonListener.actionPerformed(PutToS3Applet.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I have created an applet that opens a JFileChooser
to select a file on the click of a JButton
. It works fine when I run it in Eclipse. When I embed it in an HTML page with the applet tag nothing happens when I click the button.
Any suggestions as to why the JFileChooser
does not open in the browser would be appreciated, but my main question is how would I debug this? I haven't been able to find anything on Google about how to add a Java console to Firefox 3.6 or Chrome. Is there a way to get some kind of information as to why the JFileChooser does not open?
Debugging answered in comment below
So the console says there is an access denied exception, which I'm guessing is because I have not "signed" the applet. What should the development process be as far as signing an applet goes? Do I have to sign it with a certificate issued by a valid CA before I can test it in a browser, or is there some simple thing you can do while just testing?
Here is my code:
package com.putapplet;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class PutToS3Applet extends JApplet {
private static long maxFileSizeInBytes = 1048576;
private static String listenerUrl = "xxx";
private String listenerQueryString;
private String signedPutUrl;
private JProgressBar progressBar;
private JButton button;
private JLabel messageField;
public void init() {
setMaxFilesize(1);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
button = new JButton("Choose File...");
button.addActionListener(new ButtonListener());
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
messageField = new JLabel();
//messageField.setPreferredSize(new Dimension(300, 20));
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
JPanel topPanel = new JPanel();
topPanel.setLayout(layout);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(layout);
topPanel.add(button);
topPanel.add(progressBar);
bottomPanel.add(messageField);
setLayout(new GridLayout(2,1));
add(topPanel);
add(bottomPanel);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int showOpenDialog = fileChooser.showDialog(null, "Upload File");
if (showOpenDialog != JFileChooser.APPROVE_OPTION) return;
final File fileToUpload = fileChooser.getSelectedFile();
if (fileToUpload.length() > PutToS3Applet.maxFileSizeInBytes) {
messageField.setText("Your file must be smaller than " + getHumanReadableMaxFilesize());
return;
}
listenerQueryString = "query[filename]=" + fileToUpload.getName();
//get signed PUT url for s3
try {
URL listener = new URL(listenerUrl + listenerQueryString);
BufferedReader in = new BufferedReader(new InputStreamReader(listener.openStream()));
signedPutUrl = in.readLine().replace("http://", "https://");
messageField.setText(signedPutUrl);
} catch (Exception e) {
messageField.setText("Oops, there was a problem connecting to the server. Please try again.");
e.printStackTrace();
}
}
}
private String getHumanReadableMaxFilesize() {
return Long.toString((PutToS3Applet.maxFileSizeInBytes / 1024 / 1024)) + "MB";
}
private void setMaxFilesize(int sizeInMegaBytes) {
PutToS3Applet.maxFileSizeInBytes = PutToS3Applet.maxFileSizeInBytes * sizeInMegaBytes;
}
}
Here is the exception:
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1285)
at java.lang.System.getProperty(System.java:650)
at javax.swing.filechooser.FileSystemView.getHomeDirectory(FileSystemView.java:393)
at javax.swing.plaf.metal.MetalFileChooserUI.installComponents(MetalFileChooserUI.java:253)
at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:136)
at javax.swing.plaf.metal.MetalFileChooserUI.installUI(MetalFileChooserUI.java:126)
at javax.swing.JComponent.setUI(JComponent.java:662)
at javax.swing.JFileChooser.updateUI(JFileChooser.java:1763)
at javax.swing.JFileChooser.setup(JFileChooser.java:360)
at javax.swing.JFileChooser.<init>(JFileChooser.java:333)
at javax.swing.JFileChooser.<init>(JFileChooser.java:286)
at com.putapplet.PutToS3Applet$ButtonListener.actionPerformed(PutToS3Applet.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是一个旁注。远程调试小程序相当容易。
配置 Java 插件以允许远程调试。在不同的平台上你可以用不同的方式来实现,但在 Windows 上会是这样的。启动控制面板,选择java。单击周围直到找到 VM 选项,然后添加这些 VM 参数:
6789 将是调试端口。
在 Eclipse 中启动远程调试是通过执行类似的操作来完成的。调试配置、远程 Java 应用程序、创建新配置。选择标准(套接字连接)作为连接类型,并输入 locahost 作为地址和 6789 作为端口。
Just a side note. It's rather easy to remote debug applets.
Configure the Java plugin to allow remote debugging. You do it in different ways in different platforms, but it will be something like this on windows. Start the control panel, select java. Click around till you find VM options, and add these VM arguments:
6789 will be the debug port.
Starting the remote debugging in Eclipse is done by doing something similar to. Debug configurations, Remote Java Application, Create a new configuration. Select Standard (Socket Attach) as connection type, and enter locahost as address and 6789 as port.
考虑启用 Java 控制台,它允许您查看所有异常堆栈跟踪,希望能让您更轻松地进行调试。
此外,“appletviewer”还设计用于在浏览器之外测试小程序,包括允许您查看异常情况。
Consider enabling the Java Console which will allow you to see all the exception stack traces hopefully making it easier for you to debug.
Also "appletviewer" is designed for testing applets outside a browser including allowing you to see the exceptions.
您的 SecurityManager 很可能不允许这样做。
在 FireFox 中,您可以通过转到“工具”>“Java 控制台”来检查 java 控制台。 Java 控制台。 Firefox 应该已经设置好了。
It's very likely that your SecurityManager isn't allowing it.
In FireFox, you can check the java console by going to Tools > Java Console. Firefox should already have it set up.
实现日志记录解决方案(例如 Log4j)是另一个可能值得考虑的选项。关于如何启动和运行的一些快速教程 。
Implementing a logging solution (such as Log4j) is another option that might be worth looking at. Some quick tutorials on how to get this up and running.
当您将小程序嵌入 html 页面时,您必须对小程序 jar 文件进行签名
查看文档
oracle 文档
创建您的证书,然后使用
然后在命令提示符下键入以下命令
when you embed your applet in html page you have to sign the applet jar file
See the documentation
oracle docs
create your certificate then use
then in command prompt type the following command