When I try to print data to a file, I am getting an error dealing with security saying that I don't have permission to write to a file.

Can anyone help me out?

Here is the code

//import packages
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.applet.*;


public class SeniorProjectApplet2 extends Applet
{
//declare variables
private char initial, tempChar;
private String lastName, temp, letterTest, fullName, yearString, monthString, dayString, reservationData, initialString;
private JPanel panel1, panel2, panel3;
private JFrame frame;
private Container container;
private JTextField initialTf, lastNameTf, yearTf, monthTf, dayTf;
private JLabel message1, initialL, lastNameL, message2, dateL, message3, blank;
private JLabel[] room, period;
private Component click;
private JButton enter1, enter2, setReservation;
private JButton[][] labs;
private boolean initialIsCharacter, initialIsLetter, lastIsString, dateIsCurrent, reservationIsAvailable, monthIsValid, dayIsValid, yearIsInt, monthIsInt, dayIsInt;
private int currentYear, yearInt, monthInt, dayInt;
private StringBuffer tempSB;
private ClickListener action;


public void init()
{
//Step One - get user's first initial and last name
message1 = new JLabel("Please enter your first initial and last name below. \n");
initialL = new JLabel("First Initial: ");
lastNameL = new JLabel("Last Name: ");
initial = ' ';
lastName = " ";
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
frame = new JFrame("Reservation Program");
container = new Container();
initialTf = new JTextField(1);
lastNameTf = new JTextField(20);
enter1 = new JButton("Enter Info.");
temp = " ";
fullName = " ";
letterTest = "abcdefghijklmnopqrstuvwxyz";
action = new ClickListener();

//Step Two - get the user's desired date and period to be reserved
message2 = new JLabel("Please enter the desired date in the appropriate text fields \n");
dateL = new JLabel("Date (MM DD YYYY): ");
monthTf = new JTextField(2);
dayTf = new JTextField(2);
yearTf = new JTextField(4);
enter2 = new JButton("Enter Info.");

//Step Three - have the user select which lab he would like to use
message3 = new JLabel("Please click the button for the lab you would like to reserve \n");
blank = new JLabel("");
labs = new JButton[5][8];
for(int i = 0; i < labs.length; i++)
{
for(int j = 0; j < labs[0].length; j++)
{
labs[j] = new JButton("Reserve");
labs[j].addActionListener(action);
}
}
room = new JLabel[5];
room[0] = new JLabel("Room 2C (Tutoring Only)");
room[1] = new JLabel("Room 222");
room[2] = new JLabel("Room 223");
room[3] = new JLabel("Room 229");
room[4] = new JLabel("Room 232");
period = new JLabel[8];
period[0] = new JLabel("Period 1");
period[1] = new JLabel("Period 2");
period[2] = new JLabel("Period 3");
period[3] = new JLabel("Period 4");
period[4] = new JLabel("Period 5(C&D)");
period[5] = new JLabel("Period 6(E&F)");
period[6] = new JLabel("Period 7");
period[7] = new JLabel("Period 8");
reservationData = "";
reservationIsAvailable = true;
tempSB = new StringBuffer();

//set font of text
Font f1 = new Font("Comic Sans", Font.BOLD, 15);
message1.setFont(f1);
initialL.setFont(f1);
lastNameL.setFont(f1);
message2.setFont(f1);
dateL.setFont(f1);
message3.setFont(f1);

//set color of background
panel1.setBackground(Color.CYAN);
panel2.setBackground(Color.CYAN);
panel3.setBackground(Color.CYAN);

//add action listener to necessary components
enter1.addActionListener(action);
enter2.addActionListener(action);

//add components for Step One
panel1.setLayout(new FlowLayout());
panel1.add(message1);
panel1.add(initialL);
panel1.add(initialTf);
panel1.add(lastNameL);
panel1.add(lastNameTf);
panel1.add(enter1);

//add components for Step Two
panel2.setLayout(new FlowLayout());
panel2.add(message2);
panel2.add(dateL);
panel2.add(yearTf);
panel2.add(monthTf);
panel2.add(dayTf);
panel2.add(enter2);
panel2.add(message3);

//add components for Step Three
panel3.setLayout(new GridLayout(6,9));
panel3.add(blank);
for(int i = 0; i < period.length; i++)
{
panel3.add(period);
}
for(int i = 0; i < labs.length; i++)
{
panel3.add(room);
for(int j = 0; j < labs[0].length; j++)
{
panel3.add(labs[j]);
}
}

//disable functions until the completion of step one
yearTf.setEnabled(false);
monthTf.setEnabled(false);
dayTf.setEnabled(false);
enter2.setEnabled(false);
for(int i = 0; i < labs.length; i++)
{
for(int j = 0; j < labs[0].length; j++)
{
labs[j].setEnabled(false);
}
}

//add frame to container, pack it, set its size
container = frame.getContentPane();
container.add(panel1, BorderLayout.NORTH);
container.add(panel2, BorderLayout.CENTER);
container.add(panel3, BorderLayout.SOUTH);
frame.setSize(300, 300);
frame.pack();
frame.setVisible(true);
}

class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

//assign the variable click to the source of the ClickListener
click = (Component)e.getSource();

//responds appropriately if ClickListener was called by the enter1 button
if(click == enter1)
{
//sets boolean values to false for error checking
initialIsCharacter = false;
initialIsLetter = false;

//gets text from initial textfield and sets it to a lower case letter
temp = initialTf.getText();
temp = temp.toLowerCase();

//checks to see if the text entered in the initial text field is more or less than one character
if(temp.length() != 1)
{
message1.setText("Try Again. Enter only your first initial in the \"First Initial\" text field");
initialTf.setText("");
}
else
{
initialIsCharacter = true;
initial = temp.charAt(0);
}

//if text from initial text field is indeed one character, checks to see that that character is a letter and not a number
if(initialIsCharacter == true)
{
for(int i = 0; i < letterTest.length(); i++)
{
if(initial == letterTest.charAt(i))
{
initialIsLetter = true;
break;
}
}
if(initialIsLetter == false)
{
message1.setText("Try Again. Enter a single letter in the \"First Initial\" text field");
initialTf.setText("");
}
}

//gets text from last name text field
lastName = lastNameTf.getText();

//checks to see that there is text in the last name text field
if(lastName.length() == 0)
{
message1.setText("Please enter your last name in the \"Last Name\" text field");
}
else
{
lastIsString = true;
}

//if the values entered in the two text fields are valid, disables step one and enables step two, formats user's name and welcomes them to the program
if(initialIsLetter == true && initialIsCharacter == true && lastIsString == true)
{
temp = Character.toString(initial);
temp = temp.toUpperCase();
initialString = temp;
tempChar = lastName.charAt(0);
temp = Character.toString(tempChar);
temp = temp.toUpperCase();
tempSB.delete(0, tempSB.length());
tempSB.append(temp);
tempSB.append(lastName);
tempSB.deleteCharAt(1);
temp = tempSB.toString();
lastName = temp;
fullName = (initialString + ". " + lastName);
message1.setText("Welcome to the lab reservation program " + fullName + ".");
initialTf.setEnabled(false);
lastNameTf.setEnabled(false);
enter1.setEnabled(false);
yearTf.setEnabled(true);
monthTf.setEnabled(true);
dayTf.setEnabled(true);
enter2.setEnabled(true);
}
}

//responds appropriately if ClickListener is called by enter2 button
if(click == enter2)
{

//sets boolean values to false for error checking
dateIsCurrent = false;
monthIsValid = false;
dayIsValid = false;
yearIsInt = false;
monthIsInt = false;
dayIsInt = false;

//creates Calendar to test that the date entered has not already passed and is in the current school year, taking the end of the school year to be June 30th
Calendar now = Calendar.getInstance();
Calendar end = Calendar.getInstance();
if(now.get(Calendar.MONTH) > 6)
{
end.set(now.get(Calendar.YEAR) + 1, 6, 30);
}
else
{
end.set(now.get(Calendar.YEAR), 6, 30);
}

//gets text from date text fields and parses them to integers
yearString = yearTf.getText();
monthString = monthTf.getText();
dayString = dayTf.getText();

//attempts to parse text to integers and sets booleans to false if not parsable
//year
try
{
yearInt = Integer.parseInt(yearString);
yearIsInt = true;
}
catch(NumberFormatException n)
{
yearIsInt = false;
}
//month
try
{
monthInt = Integer.parseInt(monthString);
monthIsInt = true;
}
catch(NumberFormatException n)
{
monthIsInt = false;
}
//day
try
{
dayInt = Integer.parseInt(dayString);
dayIsInt = true;
}
catch(NumberFormatException n)
{
dayIsInt = false;
}

//tests to see that the integer entered for month is a valid value
if(monthInt > 12 || monthInt < 1)
{
monthIsValid = false;
}
else
{
monthIsValid = true;
}

//tests to see that the integer entered for day is a valid value depending on the month value entered
if(monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10 || monthInt == 12)
{
if(dayInt > 31 || dayInt < 0)
{
dayIsValid = false;
}
else
{
dayIsValid = true;
}
}
if(monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11)
{
if(dayInt > 30 || dayInt < 0)
{
dayIsValid = false;
}
else
{
dayIsValid = true;
}
}
if(monthInt == 2 && yearInt% 4 == 0)
{
if(dayInt > 29 || dayInt < 0)
{
dayIsValid = false;
}
else
{
dayIsValid = true;
}
}
if(monthInt == 2 && yearInt % 4 != 0)
{
if(dayInt > 29 || dayInt < 0)
{
dayIsValid = false;
}
else
{
dayIsValid = true;
}
}

//creates a Calendar with entered integers and tests to see that it is in the current school year
Calendar cl = Calendar.getInstance();
cl.set(yearInt, monthInt, dayInt);
if(cl.compareTo(now) > 0 && cl.compareTo(end) < 0)
{
dateIsCurrent = true;
}
else
{
dateIsCurrent = false;
}

//series of if statements that check the booleans and either lock the data or identify the problem to the user and resets the text field to blank
if(yearIsInt == true && dateIsCurrent == true && monthIsInt == true && monthIsValid == true && dayIsInt == true && dayIsValid == true)
{
yearTf.setEnabled(false);
monthTf.setEnabled(false);
dayTf.setEnabled(false);
enter2.setEnabled(false);
message2.setText("Date Set");
for(int i = 0; i < labs.length; i++)
{
for(int j = 0; j < labs[0].length; j++)
{
labs[j].setEnabled(true);
}
}
try
{
File file = new File("reservations.txt");
Scanner sc = new Scanner(file);
tempSB.delete(0, tempSB.length());
tempSB.append(yearString);
if(monthString.length() == 1)
{
monthString = ("0" + monthString);
}
tempSB.append(monthString);
if(dayString.length() == 1)
{
dayString = ("0" + dayString);
}
tempSB.append(dayString);
reservationData = tempSB.toString();
while(sc.hasNext())
{
if(reservationData.equals(sc.next()))
{
int i = Integer.parseInt(sc.next());
int j = Integer.parseInt(sc.next());
labs[j].setEnabled(false);
}
}
sc.close();
}
catch(IOException x)
{
message3.setText("File Not Found");
}
}
else
{
if(yearIsInt == false)
{
message2.setText("Please enter a four digit integer in the year text field");
yearTf.setText("");
}
if(dateIsCurrent == false)
{
message2.setText("Please enter a date in the current school year");
yearTf.setText("");
monthTf.setText("");
dayTf.setText("");
}
if(monthIsInt == false)
{
message2.setText("Please enter a two digit integer in the month text field");
monthTf.setText("");
}
if(monthIsValid == false)
{
message2.setText("Please enter a month value between 01 and 12");
monthTf.setText("");
}
if(dayIsInt == false)
{
message2.setText("Please enter a two digit integer in the day text field");
dayTf.setText("");
}
if(dayIsValid == false)
{
message2.setText("Please enter a day value that is valid with respect to the given month value");
dayTf.setText("");
}
}
}
for(int i = 0; i < labs.length; i++)
{
for(int j = 0; j < labs[0].length; j++)
{
if(click == labs[j])
{
try
{
File file = new File("reservations.txt");
FileWriter fw = new FileWriter(file, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(fullName);
pw.println(reservationData);
pw.println(i + " " + j);
pw.close();
}
catch(IOException x)
{
message3.setText("File Not Found");
}
}
}
}
}
}
}

and the html file

<html>
<object code = "SeniorProjectApplet2.class" width = 375 height = 300></object>
</html>


The purpose of the program is to allow teachers to reserve computer labs at my school. The user enters their name and a date and the program scans a file for matching dates. It then disables the buttons representing the reserved rooms. When the user clicks an enabled button to reserve a particular room for a particular time, the program is supposed to write the info to the file but again I am getting the security error that mattu36 was getting.

This is for a project for school and its due Wednesday so any help you can give me before then would be really appreciated.

Thanks a mil

carlos

No there was no particular reason. I just tried it out and it worked until it had to write the data and then i got the same error

Hi,

you need to give the applets permissions in a policy file like

grant codeBase "http://myserver/myjar.jar" {
permission java.io.FilePermission "reservations.txt", "read,write";
}

and add that policy file to the JVM startup parameter in your browser.

All you details you need to get this running, can be found here:
http://java.sun.com/j2se/1.4.2/docs/guide/security/PolicyFiles.html

You can also sign your applet and import the public key of your code signing certificate into your browser.

Cheers
M.

This article has been dead for over six months. Start a new discussion instead.