簡體   English   中英

在瀏覽器中調試java applet - 在Eclipse中工作但不在瀏覽器中工作

[英]Debugging java applet in browser - works in Eclipse but not browser

我創建了一個applet,它打開一個JFileChooser來點擊一個JButton來選擇一個文件。 當我在Eclipse中運行它時,它工作正常。 當我將它嵌入帶有applet標簽的HTML頁面時,單擊按鈕時沒有任何反應。

關於為什么JFileChooser不能在瀏覽器中打開的任何建議都會受到贊賞, 但我的主要問題是如何調試這個? 我無法在Google上找到有關如何將Java控制台添加到Firefox 3.6或Chrome的任何內容。 有沒有辦法獲得有關JFileChooser無法打開的原因的某種信息?

調試在下面的評論中回答

所以控制台說有一個訪問被拒絕的異常,我猜是因為我還沒有“簽名”applet。 在簽署applet時,開發過程應該是什么? 在我可以在瀏覽器中測試之前,是否必須使用由有效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)

只是旁注。 遠程調試applet相當容易。

配置Java插件以允許遠程調試。 你可以在不同的平台上以不同的方式實現它,但在Windows上它會是這樣的。 啟動控制面板,選擇java。 單擊“開”直到找到VM選項,然后添加以下VM參數:

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

6789將是調試端口。

在Eclipse中啟動遠程調試是通過執行類似的操作來完成的。 調試配置,遠程Java應用程序,創建新配置。 選擇Standard(Socket Attach)作為連接類型,輸入locahost作為地址,輸入6789作為端口。

考慮啟用Java控制台,這將允許您查看所有異常堆棧跟蹤,希望您可以更輕松地進行調試。

此外,“appletviewer”旨在測試瀏覽器之外的applet,包括允許您查看異常。

您的SecurityManager很可能不允許它。

在FireFox中,您可以轉到“工具”>“Java控制台”來檢查Java控制台。 Firefox應該已經設置好了。

當您在html頁面中嵌入applet時,您必須在applet jar文件中簽名。請參閱文檔oracle docs

創建您的證書然后使用然后在命令提示符下鍵入以下命令

jarsigner jarfilename.jar yourcertificatename

實現日志記錄解決方案(例如Log4j )是另一個值得關注的選項。 一些關於如何啟動和運行的快速教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM