Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Applet Error

0 views
Skip to first unread message

tramadrama

unread,
Oct 14, 2005, 11:25:18 PM10/14/05
to
Hey all, I am a newbie to forums and I'm in desperate need. I am not
familiar with Java Programming so I am trying to go through some tutorials
to help me to understand some things. One particular thing I want to learn
is using an Applet to read/write to a .txt or .dat file. Here is the
premise:

Aim: An Applet that reads/write to a .dat file.

PC Requirements: For right now, this Applet is for use with a standalone
PC without internet connection.

Code: Here's the code that I have (Don't laugh, it's my third attempt at
Java :) ):
---------------------------------------------------
/* Recipe Rolodex Database: This JAVA code is intended to make an APPLET
DATABASE to hold all of the recipes entered into the system. The database
will take user input and output information to the user through a web
browser. */

import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*; // ask group members if this is redundant to line
above

public class ROLODEX extends Applet implements ActionListener {

// Create IO Objects

DataOutputStream ostream;

public ROLODEX() {

try {

ostream = new DataOutputStream(new
FileOutputStream("recipe_database.dat"));
}

catch(IOException e) {

System.err.println("File not Opened");
System.exit(1);
}
}

// Create LABELS for the Recipe Rolodex's form interface.

Label prompt = new Label("Submit a Recipe"); // Tells user to Submit a
Recipe.
Label recipe_label = new Label("Recipe Name: "); // Recipe name text
field header.
Label category_label = new Label("Category: "); // Category text field
header.
Label type_label = new Label("Meal Type: "); // Meal type text field
header.
Label serving_label = new Label("Serving Size: "); // Serving size text
field header.
Label ingredient_label = new Label("Ingredients: "); // Ingredient text
field header.
Label preparation_label = new Label("Preparation: "); // Preparation
text field header.
Label filename_label = new Label("");

// Create TEXTFIELD for the Recipe Rolodex's form interface.

TextField recipe_text = new TextField("",35);
TextField category_text = new TextField("",7);
TextField type_text = new TextField("",35);
TextField serving_text = new TextField("",2);

// Create TEXTAREA for the Recipe Rolodex's form interface.

TextArea ingredient_area = new TextArea("");
TextArea preparation_area = new TextArea("");

// Create SUBMIT AND RESET buttons for the Recipe Rolodex's form
interface.

Button submit = new Button("Submit");
Button reset = new Button("Reset");

// Create the FONT for the Recipe Rolodex's form interface.

Font system_prompt = new Font("Arial",Font.BOLD,12);
Font system_font = new Font("Arial",Font.BOLD,10);

// Begin actual setup for the Recipe Rolodex's form interface for JAVA
APPLET.

public void init() {

// Adding the LABELS to the Recipe Rolodex's interface.

add(prompt);
add(recipe_label);
add(recipe_text);
add(category_label);
add(category_text);
add(type_label);
add(type_text);
add(serving_label);
add(serving_text);
add(ingredient_label);
add(ingredient_area);
add(preparation_label);
add(preparation_area);
add(submit);
add(reset);

// Setting the FONT to the LABELS in the Recipe Rolodex's interface.

prompt.setFont(system_prompt);
recipe_label.setFont(system_font);
category_label.setFont(system_font);
type_label.setFont(system_font);
serving_label.setFont(system_font);
ingredient_label.setFont(system_font);
preparation_label.setFont(system_font);
}

// Create the APPLET'S start screen.


// Creating the GRAPHICS for the APPLET

public void paint(Graphics g) {

setBackground(Color.white); // set background color of APPLET

// Setting LOCALE of LABELS, TEXTFIELDS, TEXTAREAS, and BUTTONS

prompt.setLocation(10,10);

recipe_label.setLocation(10,50);
recipe_text.setLocation(100,50);

category_label.setLocation(400,50);
category_text.setLocation(470,50);

type_label.setLocation(10,100);
type_text.setLocation(100,100);

serving_label.setLocation(400,100);
serving_text.setLocation(505,100);

ingredient_label.setLocation(10,150);
ingredient_area.setLocation(100,150);

preparation_label.setLocation(10,350);
preparation_area.setLocation(100,350);

submit.setLocation(265,550);
reset.setLocation(345,550);

// Add ACTIONLISTENER to the SUBMIT and RESET BUTTONS.

submit.addActionListener(this);
reset.addActionListener(this);
}

// Creating the ACTIONPERFORMED when the SUBMIT and RESET BUTTON is
pressed.

public void actionPerformed(ActionEvent thisEvent) {

if(thisEvent.getSource() == reset) {

recipe_text.setText("");
category_text.setText("");
type_text.setText("");
serving_text.setText("");
ingredient_area.setText("");
preparation_area.setText("");
}

// When the user chooses submit, this is gets the database going. It
collects the information.

if(thisEvent.getSource() == submit) {

int size;

try {

ostream.writeUTF(recipe_text.getText());
ostream.writeUTF(category_text.getText());
ostream.writeUTF(type_text.getText());
size = Integer.parseInt(serving_text.getText());
ostream.writeUTF(ingredient_area.getText());
ostream.writeUTF(preparation_area.getText());

recipe_text.setText("");
category_text.setText("");
type_text.setText("");
serving_text.setText("");
ingredient_area.setText("");
preparation_area.setText("");
}

catch(NumberFormatException e2) {

System.err.println("Invalid number of guests");
}

catch(IOException e3) {

System.err.println("Error writing file");
System.exit(1);
}
}
}
}
-----------------------------------------------------
When I try to display the Applet to a broswer, because I would still like
it in a browser regardless of not having an INET connection, it gives a
red X. and then it gives these errors:

------------------------------------------------------

Java Plug-in 1.5.0_05
Using JRE version 1.5.0_05 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\@nom.com


----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

java.security.AccessControlException: access denied
(java.io.FilePermission recipe_database.dat write)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkWrite(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at ROLODEX.<init>(ROLODEX.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.security.AccessControlException: access denied
(java.io.FilePermission recipe_database.dat write)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkWrite(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at ROLODEX.<init>(ROLODEX.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-8" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.security.AccessControlException: access denied
(java.io.FilePermission recipe_database.dat write)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkWrite(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at ROLODEX.<init>(ROLODEX.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-12" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.security.AccessControlException: access denied
(java.io.FilePermission recipe_database.dat write)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkWrite(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at ROLODEX.<init>(ROLODEX.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-16" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
----------------------------------------------------------

Does anyone know what I am doing wrong or is it even possible to do this.
I thank you for your time and effort.

Dave Glasser

unread,
Oct 14, 2005, 11:45:55 PM10/14/05
to
"tramadrama" <terre...@gmail.com> wrote on Fri, 14 Oct 2005
23:25:18 -0400 in comp.lang.java.help:

>
>java.security.AccessControlException: access denied
>(java.io.FilePermission recipe_database.dat write)

>----------------------------------------------------------
>
>Does anyone know what I am doing wrong or is it even possible to do this.
>I thank you for your time and effort.

Applets running in a browser are running in a security "sandbox" that
prevents them from doing certain things, like accessing a user's hard
drive. That way applets can't act as trojans and do malicious things
like wiping out a person's files or reading private data and sending
it back to the server that served it. The JVM makes no distinction
between applets that are served from the local machine or a remote
server in this regard. When an applet runs in the Applet viewer, it's
my understanding (which may not be 100% correct) that it has basically
all of the same security permissions that a normal Java desktop app
would have.

If you sign your applet, a user can grant it permission to write to
his/her disk. It's a rather involved process. See here for more info:

http://java.sun.com/developer/technicalArticles/Security/Signed/

--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net

Andrew Thompson

unread,
Oct 14, 2005, 11:45:15 PM10/14/05
to
tramadrama wrote:
> ..using an Applet to read/write to a .txt or .dat file. Here is the
...

> java.security.AccessControlException: access denied
> (java.io.FilePermission recipe_database.dat write)
..

> Does anyone know what I am doing wrong

Yes.

> ..or is it even possible to do this.

Yes. So long as the applet is *signed* and the user accepts
the digital signature (when prompted), this can be done.

Any particular reason you are doing this as an applet, rather
than an application?

Your use of AWT makes me suspicious.. are you intending
to run this in 'old Java'? If so, I should add that there is
only one persoan knocking around these groups, that I know of,
who has managed to sign code suitable for the old MSVM, as
well as more modern VM's.

(I suggest you toss the AWT applet and do this as a Swing application)

Roedy Green

unread,
Oct 15, 2005, 2:22:20 AM10/15/05
to
On Fri, 14 Oct 2005 23:25:18 -0400, "tramadrama"
<terre...@gmail.com> wrote or quoted :

> ostream = new DataOutputStream(new
>FileOutputStream("recipe_database.dat"));
> }
>

we have a problem. You are trying to write to the local file system.
For that you need special permission and a signed Applet. See
http://mindprod.com/jgloss/applet.html
http://mindprod.com/jgloss/signedapplets.html

You might want to tackle this as an Application rather than an Applet.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

tramadrama

unread,
Oct 15, 2005, 8:03:32 AM10/15/05
to
Thanks all for your comments. I am going to try to sign the applet first.
If that doesn't work, then I wlll just take the latter idea and make it as
an application. In fact, I have it already made as an application as a back
up plan, but just found it interesting to do this in a browser. Basically,
just trying to learn java. As I said before, I'm a beginner in Java
programming. Thanks again.

Roedy Green

unread,
Oct 15, 2005, 5:04:05 PM10/15/05
to
On Sat, 15 Oct 2005 08:03:32 -0400, "tramadrama"
<terre...@gmail.com> wrote or quoted :

>Thanks all for your comments. I am going to try to sign the applet first.

When you are learning, it is best to add complications one at a time.
Otherwise, when something does not work, you don't know if it has
something to do with signing or something to do with jar building for
example.

0 new messages