File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes access to local file system works!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "access to local file system works!!!" Watch "access to local file system works!!!" New topic

access to local file system works!!!

Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
I am sure the subject line catches a lot of attention
There is this example signed applet which can access the local file system (say displays the file size when a filename is entered in a field and the button is clicked).

The applet has a public function called getFileSize(String filename).

when it is called thru the applet, meaning: typing in a file name and clicking the button (JButton- which has a command and actionListener defined on it and which hence causes the actionPerformedEvent which is listened by the applet as it implements the listener, the getFileSize() is called and I see the size displayed fine.

However, I tried to call the same function using javascript. Since it is public function, when I do a

document.APPLETNAME.getFileSize("FILENAMEFULLPATH") should ideally return me the file size just as it will do when a name is typed in the textfield and button pressed.

But, it doesn't do that. File permission exception occurs and file access is denied.

So, finally my question is, how do I simulate an event? How do I simulate a applet buttonclicked event?

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 694
To simulate a button press, post an ActionEvent to the eventqueue:
Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163

This is what I did. However when I invoke accessFile() thru javascript, the postEvent method doesn't seem to work or it works but the actionListener code is not reached. In the api for postEvent it says a 1.1 style event is raised, I don't know if that is the issue since my versions are certainly not 1.1. What is the workaround?

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class SizeChecker extends JApplet implements ActionListener{

private JPanel pane = null;
private JScrollPane scrolling = null;
private JTextPane fileBox = null;
private JTextField tfFilename = null;
private JButton butLoad = null;
private final String LOAD = "load";
private long fileSize=0;

public void init() {
try {
} catch(Exception e) {

public String accessFile(String fn) {

String retValue="true";
try {
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(butLoad,ActionEvent.ACTION_PERFORMED,"Button

} catch (Exception e) {
return retValue;

public String readFile(String fn) {
String thisLine, ret = "";
try {
FileInputStream fin = new FileInputStream(fn);
BufferedReader myInput = new BufferedReader
(new InputStreamReader(fin));
while ((thisLine = myInput.readLine()) != null) {
ret += thisLine + "\n";
} catch (Exception e) {
ret = "Cannot load, exception!";
return ret;

private void jbInit() throws Exception {
pane = new JPanel();
pane.setBounds(new Rectangle(0, 0, 500, 325));
pane.setBackground(new Color(221, 194, 219));

fileBox = new JTextPane();
scrolling = new JScrollPane(fileBox);
scrolling.setBounds(new Rectangle(16, 65, 295, 225));

tfFilename = new JTextField();
tfFilename.setBounds(new Rectangle(16, 23, 206, 29));

butLoad = new JButton();
butLoad.setBounds(new Rectangle(231, 23, 80, 30));



public void actionPerformed(ActionEvent e) {
tfFilename.setText("Event Performed");
if (e.getActionCommand().equals(LOAD)) {


Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
I found that a simple. butLoad.doClick() would definitely raise the event. However the applet security system si smart enough to be aware fo that and whenever I do a doClick instead of manual click on the button, I get a file permission Exception.

No clue what could be the workaround
Any ideas??

Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163

says that it has been disallowed starting from 1.4.2. Meaning, javascript calls to applet methods are considered UNTRUSTED even if the applet itself is signed.

I agree. Here's the link:
subject: access to local file system works!!!
It's not a secret anymore!