问题描述:

Okay I know there are a lot of questions about this and a lot of answers but I really haven't had any luck with any of them. I have a multi-class, multi-package program that also uses some external libraries (as jar files). I exported my project as a jar file as well and below is my "index.html" that references the necessary libraries and my jar file. All those files have been put in the same directory and I can see the applet on my webpage: 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>

My main class (BasicGUI.java) extends JApplet and calls a variety of other classes based on input. It has the public void init() as well. Can anyone please tell me why this isn't totally working? Here is a link to my site where it is uploaded: http://easlnx01.eas.muohio.edu/~whitetc2/Twitter%20Mining%202/

As you can see the applet comes up but now the options panel in the file tab won't work and the program itself doesn't actually function.

Here is my main BasicGUI.java class:

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));

}

}

网友答案:

Start here:

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)

Typically an applet operates in a restrictive security sand-box. To get all properties or reach across sites, the applet needs to be digitally signed by you, and trusted by the end-user (they 'Click OK when prompted').

General tips:

  1. Configure the Java Console to pop up when an applet loads. You are 'flying blind' at the moment.
  2. For broken code, ensure that every catch calls Throwable.printStackTrace();
相关阅读:
Top