Applet无法在浏览器中运行

时间:2012-06-18 18:18:29

标签: java xml eclipse security applet

好的,我知道有很多关于这个和很多答案的问题,但我真的没有任何运气。我有一个多类,多包程序,也使用一些外部库(作为jar文件)。我将我的项目导出为jar文件,下面是我的“index.html”,它引用了必要的库和我的jar文件。所有这些文件都放在同一个目录中,我可以在我的网页上看到小程序:http://easlnx01.eas.muohio.edu/~whitetc2/Twitter%20Mining%202/

<html>
<head>
<title>Java Example</title>
</head>

<body>
<center>
This is my page<br>
Below you see an applet<br>
<br>
<applet codebase ="." code="main.BasicGUI.class"
    archive="twitter.jar, jsch-0-1.1.48.jar, twitter4j-core-2.2.5.jar, sftp.jar"
    height="600" width="450"/>
</applet>
</center>
</body>
</html> 

我的主类(BasicGUI.java)扩展了JApplet并根据输入调用了各种其他类。它也有public void init()。谁能告诉我为什么这不完全有效?以下是我上传网站的链接:http://easlnx01.eas.muohio.edu/~whitetc2/Twitter%20Mining%202/

正如您所看到的那样,applet出现了,但现在文件选项卡中的选项面板将无效,程序本身实际上无法正常运行。

这是我的主要BasicGUI.java类:

package main;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JApplet;

import com.jscape.inet.sftp.SftpException;

import Twitter.SearchTweets;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/** 
 *  The GUI for our Twitter Widget. Plans are to eventually
 *  set this up with a Java Applet to run straight from the 
 *  CC teams website.
 *  @author Taylor White and Alex Meyer
 */
public class BasicGUI extends JApplet{

    //Auto-generated 
    private static final long serialVersionUID = 1L;

    //Aesthetic options, have getters and setters to allow for communication across GUIs
    private static int opposite;
    private static int maxTweets = 100;
    private static boolean sortByLocation = false;
    private static boolean isOptionsOpen = false;

    // Options GUI, initiated here to avoid multiple open windows
    //private OptionsGUI options;

    // Initialize all swing objects.
    private JPanel northSubPnl1; 
    private JPanel northSubPnl2;
    private JPanel northSubPnl3;
    private JPanel northPnl;
    private JPanel eastPnl;
    private JPanel westPnl;
    private JLabel myQuery;

    /** TWITTER INITILIAZATIONS **/
    private static Twitter.TwitterDataList tweets;
    /** END TWITTER INITIALIZATIONS **/

    // Buttons
    private JButton queryBtn; // Submits the query
    private JButton uploadBtn;

    // TextField Query
    private JTextField txtQuery; // USER Query

    // Menu
    private JMenuBar mb; // Menu bar
    private JMenu mnuFile; // File Entry on Menu bar
    private JMenuItem mnuFileOpen;
    private JMenuItem mnuFileSave;
    private JMenuItem mnuFileOptions;
    private JMenuItem mnuItemQuit; // Quit sub item
    private JMenu mnuHelp; // Help Menu entry
    private JMenuItem mnuItemAbout; // About Entry

    //West Panel stuff
    private static JList list;
    private JScrollPane listScroller;
    private JLabel uploadText;
    private JCheckBox uploadHTML;
    private JCheckBox uploadXML;
    private JCheckBox uploadJSON;

    //East Panel stuff
    private JTextArea user;
    private static JTextArea sysStatus;
    private JScrollPane statusScroll;
    private static JTextArea settings;
    private static String keywords;

    /*
     * Getters and setters for aesthetic options
     */
    public static boolean isSortedByLocation() {
        return sortByLocation;
    }

    public static void setSortedByLocation(boolean toSort) {
        sortByLocation = toSort;
    }

    public static int getMaxTweets() {
        return maxTweets;
    }

    public static void setMaxTweets(int num) {
        maxTweets = num;
    }

    public static boolean isOptionsOpen() {
        return isOptionsOpen;
    }

    public static void setOptionsOpen(boolean option) {
        isOptionsOpen = option;
    }

    /*
     * CONSTRUCTOR
     */
    public void init() {
        sysStatus = new JTextArea (1, 20);
        sysStatus.setEditable(false);
        northSubPnl1 = new JPanel(); 
        northSubPnl2 = new JPanel();
        northSubPnl3 = new JPanel();
        northPnl = new JPanel();
        eastPnl = new JPanel();
        westPnl = new JPanel();
        myQuery = new JLabel ("You have no queries yet");

        // TwitterDataList
        tweets = new Twitter.TwitterDataList ();

        // Buttons
        queryBtn = new JButton("Submit Query"); // Submits the query
        uploadBtn = new JButton("Upload Tweets"); // Uploads both XML and HTML files.

        // TextField Query
        txtQuery = new JTextField(); // USER Query

        // Menu
        mb = new JMenuBar(); // Menu bar
        mnuFile = new JMenu("File");
        mnuFileOpen = new JMenuItem("Open"); 
        mnuFileSave = new JMenuItem("Save");
        mnuFileOptions = new JMenuItem("Options");
        mnuItemQuit = new JMenuItem("Quit"); 
        mnuHelp = new JMenu("Help"); 
        mnuItemAbout = new JMenuItem("About"); 

        //West Panel stuff
        String[] tweetsString = new String[2000];
        list = new JList (tweetsString);
        uploadText = new JLabel("Upload as:");
        uploadHTML = new JCheckBox("HTML", false);
        uploadXML = new JCheckBox("XML", false);
        uploadJSON = new JCheckBox("JSON", false);

        //East Panel stuff
        user = new JTextArea("Username: ");
        keywords = new String ();
        statusScroll = new JScrollPane(sysStatus);
        statusScroll.setPreferredSize(new Dimension(200, 150));
        statusScroll.getVerticalScrollBar().setValue(statusScroll.getVerticalScrollBar().getMaximum());
        settings = new JTextArea(1, 20);


        // Set menu bar
        this.setJMenuBar(mb);

        //Build Menus
        this.buildMenus();
        // West Panel
        this.westPnlSetup();
        // East Panel
        this.eastPnlSetup();
        // Add objects to respective panels
        this.northPnlSetup();
        // Setup Main Frame
        this.setLayout();
        // Add Listeners
        this.addListeners();

        //Finish Launch
        Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int width = (int)dim.getWidth();
        int height = (int)dim.getHeight();;                     //worry about location later. This is fine for testing purposes
        this.setBounds(width/2-400, height/2-400, 500, 800);    //Arbitrarily chosen for a 1600 by 900 screen 
        this.setVisible(true);
    }

    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == queryBtn) {        
                if (txtQuery.getText().length() > 2) {
                    myQuery.setText("Your current query: " + txtQuery.getText());
                    tweets = Twitter.SearchTweets.customSearch(txtQuery.getText()); // SEARCH
                    if (SearchTweets.getKeywords() != null) {
                        if (tweets.getSize() > 0)
                            tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords()); // Sort right after.
                        else {
                            print("No tweets found!");
                            System.out.println("No tweets found!");
                        }
                    }
                    if (tweets.getSize() > 0) {
                        list.setListData(tweets.toArray(maxTweets));
                        sysStatus.append("Top Tweet: " + tweets.get(0).getTweet());
                        System.out.println("Top Tweet: " + tweets.get(0).getTweet());
                        //tweets.saveHTML(txtQuery.getText()); NOT SAVING BY DEFAULT ANY MORE
                    }
                    else {
                        print("No tweets found!");
                        System.out.println("No tweets found!");
                    }
                }
                else {
                    print("Please input at least a 3 letter word to begin a query.");
                    System.out.println("Please input at least a 3 letter word to begin a query.");
                }
            }

            // Not sure how to make directory path independent of system. change path to your local project to get open and save to work
            else if (e.getSource() == mnuFileOpen) {
                final JFileChooser fc = new JFileChooser("C:/Users/Meyer/Desktop/YATE/DataCollection");
                int returnVal = fc.showOpenDialog(mnuFile);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    tweets = Twitter.TwitterDataList.load(file.getName());
                    if (SearchTweets.getKeywords() != null) {
                        tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords());
                    }
                    list.setListData(tweets.toArray(maxTweets));
                    myQuery.setText("Your current query: " + file.getName());
                }
            }

            else if (e.getSource() == mnuFileSave) {
                final JFileChooser fc = new JFileChooser("C:/Users/Meyer/Desktop/YATE/DataCollection");
                int returnVal = fc.showSaveDialog(mnuFile);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    tweets.writeHTML(file.getName());
                }
            }

            else if (e.getSource() == mnuFileOptions) {
                if (!isOptionsOpen()) {
                    new OptionsGUI();
                    setOptionsOpen(true);
                }           
            }

            else if (e.getSource() == uploadBtn) {
                if (uploadHTML.isSelected() || 
                        uploadXML.isSelected() || uploadJSON.isSelected()) {
                    try {
                        tweets.saveAll(txtQuery.getText(), uploadHTML.isSelected(), 
                                uploadXML.isSelected(), uploadJSON.isSelected());
                    } catch (SftpException exception) {
                        System.out.println("Could not upload. Try another time.");
                        print("Could not upload. Try another time.");
                    }
                }
                else if (tweets.getSize() ==0) {
                    JOptionPane.showMessageDialog(null, "There are no tweets to upload!");
                }
                else {
                    JOptionPane.showMessageDialog(null, "You need to select a file type in order to upload.");
                }
            }
        }
    }

    public class KeyChecker implements KeyListener {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) 
                queryBtn.doClick();
        }

        public void keyReleased(KeyEvent arg0) {
            //ignore
        }

        public void keyTyped(KeyEvent arg0) {
            //ignore
        }
    }

    public class ListListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {    
            try {
                user.setText(tweets.get(list.getSelectedIndex()).toString());
            } catch (ArrayIndexOutOfBoundsException error) {
                user.setText(tweets.get(list.getFirstVisibleIndex()).toString());
            }
        }
    }

    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);         
        }
    }

    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);         
        }
    }

    public void buildMenus () {
        mnuFile.add(mnuFileOpen);
        mnuFile.add(mnuFileSave);
        mnuFile.add(mnuFileOptions);
        mnuFile.add(mnuItemQuit);  // Create Quit line
        mnuHelp.add(mnuItemAbout); // Create About line
        mb.add(mnuFile);        // Add Menu items to form
        mb.add(mnuHelp);
    }


    /**
     * North Panel
     */
    public void northPnlSetup () {
        northPnl.setLayout(new BoxLayout (northPnl, BoxLayout.Y_AXIS));
        northSubPnl1.add(txtQuery);
        northSubPnl1.add(queryBtn);
        txtQuery.setColumns(20);
        northPnl.add(northSubPnl1);

    }
    /**
     * Sets Layout of GUI
     */

    public void setLayout () {
        // Setup Main Frame
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(northPnl, BorderLayout.NORTH);
        this.getContentPane().add(westPnl, BorderLayout.WEST);
        this.getContentPane().add(eastPnl, BorderLayout.EAST);
    }

    /*
     * Adds action listeners    
     */
    public void addListeners(){
        mnuItemQuit.addActionListener(new ListenMenuQuit());
        mnuFileOpen.addActionListener(new ButtonListener());
        mnuFileSave.addActionListener(new ButtonListener());
        mnuFileOptions.addActionListener(new ButtonListener());
        queryBtn.addActionListener(new ButtonListener());
        uploadBtn.addActionListener(new ButtonListener());
        list.addListSelectionListener(new ListListener());
        txtQuery.addKeyListener(new KeyChecker());

    }

    public void westPnlSetup () {
        westPnl.add(new JLabel ("Tweets"));
        westPnl.add(new JLabel("-------"));
        westPnl.setLayout(new BoxLayout (westPnl, BoxLayout.Y_AXIS));
        listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(200, 100));
        list.setListData(tweets.toArray(maxTweets));
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL);
        list.setVisibleRowCount(10);
        westPnl.add(listScroller);
        // setup upload button and panel for check boxes
        northSubPnl2.setLayout(new BoxLayout(northSubPnl2, BoxLayout.Y_AXIS));
        northSubPnl2.add(uploadBtn);
        uploadBtn.setAlignmentX(CENTER_ALIGNMENT);
        northSubPnl2.add(uploadText);
        uploadText.setAlignmentX(CENTER_ALIGNMENT);
        northSubPnl2.add(northSubPnl3);
        // setup check boxes
        northSubPnl3.setLayout(new BoxLayout(northSubPnl3, BoxLayout.X_AXIS));
        northSubPnl3.add(uploadHTML);
        northSubPnl3.add(uploadXML);
        northSubPnl3.add(uploadJSON);
        westPnl.add(northSubPnl2);
    }

    public void eastPnlSetup () {
        eastPnl.setLayout(new BoxLayout (eastPnl, BoxLayout.Y_AXIS));
        eastPnl.add(new JLabel("Metadata"));
        eastPnl.add(new JLabel("-------"));
        user = new JTextArea(1, 20);
        user.setPreferredSize(new Dimension (200,50));
        user.setEditable(false);
        user.append("User: \n");
        user.append("Date: \n");
        user.append("Location: \n");
        user.append("Relevance: \n");
        user.append("Tweet: ");
        user.setWrapStyleWord(true);
        user.setLineWrap(true);
        eastPnl.add(user);
        eastPnl.add(new JLabel("Settings"));
        eastPnl.add(new JLabel(new JLabel("-------").getText()));
        String keywords = "";
        for (int i = 0; i < SearchTweets.getKeywords().size(); i++) {
            keywords = SearchTweets.getKeywords().get(i) + ", ";
        }
        try {
            keywords = keywords.substring(0, keywords.length()-2);
        } catch (StringIndexOutOfBoundsException e) {
            keywords = ""; // There are no keywords if this happens.
        }

        /*
         * || THIS IS IF IN WHITE ||
         * ||                     ||
         * VV                     VV
         */
        settings.setPreferredSize(new Dimension (200,50));
        settings.setEditable(false);
        BasicGUI.setSettingsPaneValues();
        settings.setWrapStyleWord(true);
        settings.setLineWrap(true);
        eastPnl.add(settings);

        sysStatus.append(">");
        eastPnl.add(new JLabel("System Status       "));
        eastPnl.add(new JLabel(new JLabel("-------").getText()+"----"));
        sysStatus.setWrapStyleWord(true);
        sysStatus.setLineWrap(true);
        eastPnl.add(statusScroll);
        //this.redirectSystemStreams(); // REDIRECTS the text of console to the sysStatus box. NOT REAL TIME.

        /* END WHITE */
    }


    public static void setSettingsPaneValues () {
        keywords = new String();
        for (int i = 0; i < SearchTweets.getKeywords().size(); i++) {
            keywords += SearchTweets.getKeywords().get(i) + ", ";
        }
        if (SearchTweets.getKeywords().size()== 0) {
            keywords = "none";
        }
        else {
            try {
                keywords = keywords.substring(0, keywords.length()-2);
            } catch (StringIndexOutOfBoundsException e) {
                keywords = ""; // There are no keywords if this happens.
            }
        }

        settings.setText("Sort by location: " + isSortedByLocation() +
                "\nTimeout (in seconds): " + SearchTweets.getTimeout() + 
                "\nMaximum tweets shown: " + getMaxTweets() + 
                "\nKeywords: " + keywords);
        if (SearchTweets.getKeywords() != null && tweets.getSize() > 0) {
            tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords());
        }
        list.setListData(tweets.toArray(maxTweets));
        list.repaint();
    }


    /**
     * returns string value of the keywords used
     * @return returns instance variable keywords
     * @author Taylor
     */
    public static String getKeywords () {
        return keywords;
    }
    /**
     * Updates the sysStatus textArea
     * @param text
     */
    private void updateTextArea(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                opposite++;
                opposite = opposite%2;
                if (!text.equalsIgnoreCase(".")){ // So I can do the ". . ." sequence to signify waiting.
                    if (opposite%2 == 1) {
                        sysStatus.append("\n>" + text);
                    }
                    else sysStatus.append(text);
                }
                else sysStatus.append(text);
            }
        });
    }


    /**
     * Allows other classes to print to sysStatus
     */
    public static void print (String text) {
        opposite++;
        opposite = opposite%2;
        if (!text.equalsIgnoreCase(".")){ // So I can do the ". . ." sequence to signify waiting.
            //if (opposite%2 == 1) {
            sysStatus.append("\n\n>" + text);
            sysStatus.repaint();
            //}
            //else sysStatus.append("\n" + text);
        }
        else sysStatus.append(text);

        sysStatus.invalidate();
        sysStatus.repaint();
    }

    /**
     * Redirects all console output to the textarea.
     */
    @SuppressWarnings("unused")
    private void redirectSystemStreams() {
        OutputStream out = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                updateTextArea(String.valueOf((char) b));
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                updateTextArea(new String(b, off, len));
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }
        };

        System.setOut(new PrintStream(out, true));
        System.setErr(new PrintStream(out, true));
    }
}

1 个答案:

答案 0 :(得分:2)

从这里开始:

Exception in thread "AWT-EventQueue-2" java.lang.AssertionError: java.lang.reflect.InvocationTargetException
    at twitter4j.TwitterFactory.<clinit>(TwitterFactory.java:76)
    ...
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ... 42 more
Caused by: java.lang.ExceptionInInitializerError
    at twitter4j.internal.http.HttpClientWrapper.<init>(HttpClientWrapper.java:48)
    ... 47 more
Caused by: java.security.AccessControlException: access denied 
        (java.util.PropertyPermission twitter4j.http.httpClient read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)

通常,applet在限制性安全沙箱中运行。要获取所有属性或跨站点访问,applet需要由您进行数字签名,并由最终用户信任(他们在提示时单击“确定”)。

一般提示:

  1. 配置Java控制台以在加载小程序时弹出。你现在正在“盲目”。
  2. 对于损坏的代码,请确保每次捕获调用Throwable.printStackTrace();