0

I was having an issue with an embedded applet I've been working on for a while now. The applet's jar is signed and all-permissions are granted, and it runs perfectly fine in the applet viewer. When loaded in the browser there is only a blank box with an error logo where my applet should be. When clicked it returns "java.lang.invocationtargetexception". I am using a basic HTML format to embed the applet. Here is the HTML Code I am using to display it:

<html>
<body background="BG.png">

<a href="index.html">Back to the index</a>

<center><h1>Web Forum</h1></center>
<center><table border = "0">

<tr>
<td><center><h3>Forums</h3></center></td>
<td width = 25></td>
<td width = 225><center><h3>Chatbox</h3></center></td>
</tr>

<tr>
<td><center><applet
code=Topics.class
archive=Topics.jar
width=750
height=1000 ><PARAM name="permissions" value="sandbox"></PARAM>
</applet></center></td>

<td width = 25></td>

<td width = 225 bgcolor = #00FF00><center><h3>Coming Soon</h3></center></td>
</tr>

</table></center>
</Body>
</Html> 

And here is the Java console dump thread list:

Dump thread list ...
Group main,ac=19,agc=2,pri=10
    main,5,alive
    traceMsgQueueThread,5,alive,daemon
    Timer-0,5,alive
    AWT-Shutdown,5,alive
    AWT-Windows,6,alive,daemon
    Java Plug-In Pipe Worker Thread (Client-Side),5,alive,daemon
    AWT-EventQueue-0,6,alive
    SysExecutionTheadCreator,5,alive,daemon
    MemoryCache-DelayedCleanup,5,alive,daemon
    CacheMemoryCleanUpThread,5,alive,daemon
    Browser Side Object Cleanup Thread,5,alive
    JVM[id=1]-Heartbeat,5,alive,daemon
Group Plugin Thread Group,ac=4,agc=0,pri=10
    AWT-EventQueue-1,6,alive
    Image Fetcher 0,8,alive,daemon
    TimerQueue,5,alive,daemon
    SysExecutionThead,5,alive,daemon
Group http://25.2.202.42/-threadGroup,ac=4,agc=0,pri=4
    Applet 1 LiveConnect Worker Thread,4,alive
    AWT-EventQueue-2,4,alive
    TimerQueue,4,alive,daemon
    ConsoleTraceListener,4,alive,daemon
Done.

Here is the signed jar's manifest:

Manifest-Version: 1.0

Name: .classpath
SHA-256-Digest: IhqMeyTH+t/Wtxr+NP6B1f5GapLwQ67HJhYQ7UM+kA0=

Name: .project
SHA-256-Digest: Lw9C+yAb3p1vC6m2pwcjhv8TB1ET8xOYjM2sFlgIjIw=

Permissions: all-permissions
Codebase: http://25.2.202.42/

I see no reason the error would be resulting form my code itself, but here it is just incase:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.applet.*;
import java.io.*;


@SuppressWarnings("serial")
public class Topics extends Applet {

String name;
String topics;
String paragraphs;
//reading file for existing posts
String fileread = "Topics.txt";
static String line = null;
{
try {
    FileReader fileReader = 
        new FileReader(fileread);

    BufferedReader bufferedReader = 
        new BufferedReader(fileReader);

    while((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }   

    bufferedReader.close();         
}
catch(FileNotFoundException ex) {
    System.out.println(
        "Unable to open file '" + 
        fileread + "'");                
}
catch(IOException ex1) {
    System.out.println(
        "Error reading file '" 
        + fileread + "'");                  

}
}
static String display = line;

JPanel topicPanel;
JLabel title, username, topic, paragraph, topicsTitle, dtopicsTitle;
JTextField nameField, topicField;
JButton submitButton, topicrefreshButton;
JTextArea paragraphArea, topicArea;


public void init() {
    final JPanel topicGUI = new JPanel();
    topicGUI.setLayout(null);

    setBackground(Color.GREEN);

    setVisible(true);

    // JLabels

    JLabel title = new JLabel("                                                                           Make A Post                                                                           ");
    title.setSize(150, 25);
    title.setFont(new Font("Serif", Font.PLAIN, 25));
    title.setHorizontalAlignment(0);
    add(title);


    JLabel username = new JLabel("Username: ");
    username.setSize(70, 15);
    username.setHorizontalAlignment(0);
    add(username);

    // JTextField

    nameField = new JTextField(8);
    nameField.setSize(150, 18);
    add(nameField);

    // JLabel

    JLabel topic = new JLabel("Topic: ");
    topic.setSize(40, 15);
    topic.setHorizontalAlignment(0);
    add(topic);

    // JTextField

    topicField = new JTextField(8);
    topicField.setSize(180, 18);
    add(topicField);


    //JLabel

    JLabel paragraph = new JLabel("                                                                                          Paragraph:                                                                                          ");
    paragraph.setSize(70, 15);
    paragraph.setHorizontalAlignment(0);
    add(paragraph);

    // JTextAreas

    paragraphArea = new JTextArea(8, 5);
    paragraphArea.setSize(700, 100);
    paragraphArea.setLineWrap(true);
    paragraphArea.setEditable(true);
    add(paragraphArea);


    // JButton

    JButton submitButton = new JButton("SUBMIT");
    submitButton.setSize(100, 30);
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            name = nameField.getText();
            topics = topicField.getText();
            paragraphs = paragraphArea.getText();



            display ="\n" + "\t" + name + " - " + "\n" + "\t" + topics + " : " + "\n" + paragraphs + display + "\n";

            topicField.setText("");
            paragraphArea.setText("");

            //writing new info to file
            String fileName = "Topics.txt";

            try {
                FileWriter fileWriter =
                    new FileWriter(fileName);

                BufferedWriter bufferedWriter =
                    new BufferedWriter(fileWriter);

                bufferedWriter.write(display);

                bufferedWriter.close();
            }
            catch(IOException ex) {
                System.out.println(
                    "Error writing to file '"
                    + fileName + "'");
            }
            topicArea.setText(display);
        }
     });


add(submitButton);
JLabel dtopicsTitle = new JLabel("                                                                           Topics:                                                                           ");
dtopicsTitle.setLocation(170, 3);
dtopicsTitle.setSize(150,30);
dtopicsTitle.setFont(new Font("Serif", Font.PLAIN, 25));
dtopicsTitle.setHorizontalAlignment(0);
add(dtopicsTitle);


topicrefreshButton = new JButton("Refresh Topics");
topicrefreshButton.setLocation(250, 30);
topicrefreshButton.setSize(100, 30);
topicrefreshButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {

        topicArea.setText(display);

    }
});
add(topicrefreshButton);    


        topicArea = new JTextArea(40, 64);
        topicArea.setSize(650, 430);
        topicArea.setLineWrap(true);
        topicArea.setEditable(false);
        topicArea.setText(display);
        add(topicArea);
        JScrollPane topicScroll = new JScrollPane(topicArea);
        topicScroll.setSize(650,430);
        add(topicScroll);


}
}

Thank you for any support you can offer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dimfish
  • 3
  • 5
  • BTW - now glance at 'anything besides the code' I note that the applet is indeed signed and trusted. But use an `URL` anyway. – Andrew Thompson Jan 02 '14 at 06:22
  • Shouldn't `Permissions` and `Codebase` be in the main section rather than an individual section of their own? (i.e. move them up to `Manifest-Version` without a blank line preceding.) – Tom Hawtin - tackline Jan 04 '14 at 15:04

1 Answers1

1

Applet and File mix like oil and water (i.e. 'not').

Not only would any File object not point where you expect (they point to the user's local file-system, rather than the server), but the applet would need to be digitally signed, and trusted, before it could use files.

Applets would typically access resources by URL. If the URL can be formed relative to the code base, document base, or class-path of the applet, it can remain sand-boxed and still work. Otherwise it will need to be trusted.

Other tips

  1. Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.
  2. Change code of the form catch (Exception e) { .. to catch (Exception e) { e.printStackTrace(); // very informative! ..
  3. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.
  4. Don't mix Swing components with AWT components. (Use JApplet instead of Applet.)
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433