问题描述
我很新到Java:
我创建了打开JFileChooser,以选择一个JButton的点击文件的小程序。当我在Eclipse中运行它,它工作正常。当我的小程序标签嵌入在HTML页面中没有任何反应,当我点击该按钮。
任何建议,为什么JFileChooser中不会在浏览器中打开将AP preciated,但是我的主要问题是如何将我调试这个?我一直没能找到在谷歌的任何有关如何将Java控制台添加到Firefox 3.6或Chrome。有没有一种方法来获得某种信息,为什么JFileChooser中不开?
的调试下面评论回答的
所以控制台说,有一个拒绝访问异常,这我猜是因为我还没有签字的小程序。我应该在开发过程中就签订一个applet去?我一定要通过有效的CA颁发的证书签字之前,我可以在浏览器中测试它,或者是有,而只是测试你可以做一些简单的事情吗?
下面是我的code:
包com.putapplet;进口java.awt.FlowLayout中;
进口java.awt.GridLayout中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.io.BufferedReader中;
进口的java.io.File;
进口java.io.InputStreamReader中;
进口的java.net.URL;进口javax.swing.JApplet中;
进口javax.swing.JButton中;
进口javax.swing.JFileChooser中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;
进口javax.swing.JProgressBar中;
进口javax.swing.SwingUtilities中;
@燮pressWarnings(串行)
公共类PutToS3Applet扩展JApplet的{私有静态长maxFileSizeInBytes = 1048576;私人静态字符串listenerUrl =XXX;
私人字符串listenerQueryString;私人字符串signedPutUrl;JProgressBar的私人进度;
私人的JButton按钮;
私人的JLabel messageField;公共无效的init(){ setMaxFilesize(1); 尝试{
SwingUtilities.invokeAndWait(新的Runnable(){
公共无效的run(){
createGUI();
}
});
}赶上(例外五){
通信System.err.println(createGUI没有成功完成);
}
}私人无效createGUI(){ 按钮=的新的JButton(选择文件...);
button.addActionListener(新ButtonListener()); 进度= JProgressBar的新(0,100);
progressBar.setStringPainted(真); messageField =新的JLabel();
//messageField.set$p$pferredSize(new尺寸(300,20)); 的FlowLayout布局=新的FlowLayout();
layout.setAlignment(FlowLayout.LEFT); JPanel的topPanel =新JPanel();
topPanel.setLayout(布局); JPanel的bottomPanel =新JPanel();
bottomPanel.setLayout(布局); topPanel.add(按钮);
topPanel.add(进度);
bottomPanel.add(messageField); 的setLayout(新的GridLayout(2,1));
加(topPanel);
加(bottomPanel);}类ButtonListener实现的ActionListener {
公共无效的actionPerformed(ActionEvent的AE){
文件选择器的JFileChooser =新的JFileChooser();
INT showOpenDialog = fileChooser.showDialog(NULL,上传文件);
如果(showOpenDialog = JFileChooser.APPROVE_OPTION!)回报; 最后文件fileToUpload = fileChooser.getSelectedFile(); 如果(fileToUpload.length()> PutToS3Applet.maxFileSizeInBytes){
messageField.setText(您的文件必须小于+ getHumanReadableMaxFilesize());
返回;
} listenerQueryString =查询[文件名] =+ fileToUpload.getName(); //获得S3签署PUT网址
尝试{ URL监听器=新的URL(listenerUrl + listenerQueryString);
在的BufferedReader =新的BufferedReader(新的InputStreamReader(listener.openStream()));
。signedPutUrl = in.readLine()取代(的http://,https://开头);
messageField.setText(signedPutUrl); }赶上(例外五){
messageField.setText(哎呀,有连接到服务器时出现问题,请再试一次。);
e.printStackTrace();
}
}
}私人字符串getHumanReadableMaxFilesize(){
返回Long.toString((PutToS3Applet.maxFileSizeInBytes / 1024/1024))+MB;
}私人无效setMaxFilesize(INT sizeInMegaBytes){
PutToS3Applet.maxFileSizeInBytes = PutToS3Applet.maxFileSizeInBytes * sizeInMegaBytes;
}
}
下面是个例外:
异常螺纹AWT-EventQueue的-1java.security.AccessControlException:访问被拒绝(java.util.PropertyPermission的user.home读)
在java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
在java.security.AccessController.checkPermission(AccessController.java:546)
在java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
在java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1285)
在java.lang.System.getProperty(System.java:650)
在javax.swing.filechooser.FileSystemView.getHomeDirectory(FileSystemView.java:393)
在javax.swing.plaf.metal.MetalFileChooserUI.installComponents(MetalFileChooserUI.java:253)
在javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:136)
在javax.swing.plaf.metal.MetalFileChooserUI.installUI(MetalFileChooserUI.java:126)
在javax.swing.JComponent.setUI(JComponent.java:662)
在javax.swing.JFileChooser.updateUI(JFileChooser.java:1763)
在javax.swing.JFileChooser.setup(JFileChooser.java:360)
在javax.swing.JFileChooser中的<&初始化GT;(JFileChooser.java:333)
在javax.swing.JFileChooser中的<&初始化GT;(JFileChooser.java:286)
在com.putapplet.PutToS3Applet $ ButtonListener.actionPerformed(PutToS3Applet.java:82)
在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
在javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2318)
在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
在javax.swing.DefaultButtonModel.set pressed(DefaultButtonModel.java:242)
在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
在java.awt.Component.processMouseEvent(Component.java:6289)
在javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
在java.awt.Component.processEvent(Component.java:6054)
在java.awt.Container.processEvent(Container.java:2041)
在java.awt.Component.dispatchEventImpl(Component.java:4652)
在java.awt.Container.dispatchEventImpl(Container.java:2099)
在java.awt.Component.dispatchEvent(Component.java:4482)
在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
在java.awt.Container.dispatchEventImpl(Container.java:2085)
在java.awt.Component.dispatchEvent(Component.java:4482)
在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
在java.awt.EventQueue.access $ 000(EventQueue.java:85)
在java.awt.EventQueue中的$ 1.run(EventQueue.java:603)
在java.awt.EventQueue中的$ 1.run(EventQueue.java:601)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:87)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:98)
在java.awt.EventQueue中的$ 2.run(EventQueue.java:617)
在java.awt.EventQueue中的$ 2.run(EventQueue.java:615)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:87)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
只是一个方面说明。这是比较容易远程调试小程序。
配置Java插件,以便远程调试。你这样做,在不同的平台不同的方式,但它会是这样的窗口。启动控制面板,选择Java。按一下周围,直到你找到VM选项,并添加这些VM参数:
-Xdebug -Xnoagent -Djava.compiler = NONE -Xrunjdwp:交通= dt_socket,服务器= Y,地址= 6789,暂停= N
6789将调试端口。
启动Eclipse中的远程调试是通过做类似的东西做的。调试配置,远程Java应用程序,创建一个新的配置。选择标准(套接字连接)作为连接类型,并输入作为的locahost地址和6789用作端口。
I am very new to Java:
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)
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:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=6789,suspend=n
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小程序 - 工程在Eclipse中而不是浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!