Hi all, i am having a runtime error with permission and have not been able to find a solution to this. I have looked everywhere and the things that i have found i do not fully understand what i need to do or what the problem actually is. I really hope someone can help with this, i am getting desperate. I need the applet to run on client and server and write to a file. The runtime error is as follows:
java.security.AccessControlException: access denied (java.io.FilePermission usernumber.txt 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 java.io.FileWriter.<init>(Unknown Source)
at Perception.init(Perception.java:83)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
My code is as follows:
import java.applet.Applet;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JSlider;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class Perception extends Applet {
private static final long serialVersionUID = 1L;
private JTextArea bodyTxt = null;
private JSlider jSlider = null;
private JLabel headlineLbl = null;
private JButton scr1Btn = null;
private JButton scr2Btn = null;
private JLabel sliderLbl1 = null;
private JLabel sliderLbl2 = null;
private JLabel sliderLbl3 = null;
private Question currentQuestion=null;
private JComboBox connectionSpeedCombo = null;
private JComboBox sexCombo = null;
private JComboBox itExperienceCombo = null;
private JComboBox sectorCombo = null;
private JButton startQuiz = null;
private int totalQuestions = 0;
private int correctAnswers = 0;
Random randomGenerator = new Random(); // @jve:decl-index=0:
int delay =0;
int userno =0;
int noOfTimesAttended = 0;
String answer ="";
// Add new questions here:
Question Q1 = new Question("What is the Capital of China? ","beijing","hongkong","Islamabad","Columbo",'A'); // @jve:decl-index=0:
Question Q2 = new Question("What is the Capital of Srilanka? ","beijing","hongkong","Islamabad","Columbo",'D');
ArrayList <Question>questionSet = new ArrayList<Question>(); // @jve:decl-index=0:
ArrayList <Question>questionSetBackup = null;
private JButton choiceA = null;
private JButton choiceB = null;
private JButton choiceC = null;
private JButton choiceD = null;
private JButton continueBtn = null;
private JComboBox performanceCombo = null;
private JComboBox documentationCombo = null;
private JComboBox reliabilityCombo = null;
private JComboBox usabilityCombo = null;
private JButton StartAgain = null;
/**
* This is the default constructor
*/
public Perception() {
super();
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
try{
Scanner sc = new Scanner(new File("usernumber.txt"));
userno = sc.nextInt();
}catch(Exception e){}
userno++;
try {
BufferedWriter out = new BufferedWriter(new FileWriter("usernumber.txt"));
out.write(userno+" ");
out.close();
} catch (IOException e) {
}
questionSet.add(Q1);
questionSet.add(Q2);
sliderLbl3 = new JLabel();
sliderLbl3.setBounds(new Rectangle(530, 260, 102, 28));
sliderLbl3.setText("Very Angry");
sliderLbl2 = new JLabel();
sliderLbl2.setBounds(new Rectangle(333, 261, 107, 25));
sliderLbl2.setText("Mildly anoyed");
sliderLbl1 = new JLabel();
sliderLbl1.setBounds(new Rectangle(165, 262, 101, 24));
sliderLbl1.setText("Dont Care");
sliderLbl1.setVisible(false);
sliderLbl2.setVisible(false);
sliderLbl3.setVisible(false);
bodyTxt = new JTextArea();
bodyTxt.setBounds(new Rectangle(238, 99, 257, 115));
bodyTxt.setEditable(false);
headlineLbl = new JLabel();
headlineLbl.setBounds(new Rectangle(289, 38, 488, 27));
this.setLayout(null);
this.setSize(800, 500);
this.add(headlineLbl, null);
this.add(getJSlider(), null);
this.add(bodyTxt, null);
this.add(getScr1Btn(), null);
this.add(getScr2Btn(), null);
this.add(sliderLbl1, null);
this.add(sliderLbl2, null);
this.add(sliderLbl3, null);
this.add(getConnectionSpeedCombo(), null);
this.add(getSexCombo(), null);
this.add(getItExperienceCombo(), null);
this.add(getSectorCombo(), null);
this.add(getStartQuiz(), null);
this.add(getChoiceA(), null);
this.add(getChoiceB(), null);
this.add(getChoiceC(), null);
this.add(getChoiceD(), null);
this.add(getContinueBtn(), null);
this.add(getPerformanceCombo(), null);
this.add(getDocumentationCombo(), null);
this.add(getReliabilityCombo(), null);
this.add(getUsabilityCombo(), null);
this.add(getStartAgain(), null);
gotoScreen1();
}
void gotoScreen1()
{
answer = "\n"+userno +"("+noOfTimesAttended+")";
headlineLbl.setText("Instructions");
bodyTxt.setText("This is an Experiment to Test IQ Levels\n of Internet users, to make the results\n meaningfull please enter some non-\npersonal information about yourself");
scr2Btn.setVisible(false);
connectionSpeedCombo.setVisible(false);
sexCombo.setVisible(false);
itExperienceCombo.setVisible(false);
sectorCombo.setVisible(false);
startQuiz.setVisible(false);
choiceA.setVisible(false);
choiceB.setVisible(false);
choiceC.setVisible(false);
choiceD.setVisible(false);
continueBtn.setVisible(false);
performanceCombo.setVisible(false);
documentationCombo.setVisible(false);
reliabilityCombo.setVisible(false);
usabilityCombo.setVisible(false);
StartAgain.setVisible(false);
headlineLbl.setVisible(true);
}
/**
* This method initializes jSlider
*
* @return javax.swing.JSlider
*/
private JSlider getJSlider() {
if (jSlider == null) {
jSlider = new JSlider();
jSlider.setVisible(false);
jSlider.setBounds(new Rectangle(168, 298, 428, 16));
}
return jSlider;
}
/**
* This method initializes scr1Btn
*
* @return javax.swing.JButton
*/
private JButton getScr1Btn() {
if (scr1Btn == null) {
scr1Btn = new JButton();
scr1Btn.setBounds(new Rectangle(455, 221, 146, 31));
scr1Btn.setText("Start Experiment>>");
scr1Btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
bodyTxt.setText("When the Internet Crashes\nHow does it make you feel.");
headlineLbl.setVisible(false);
scr1Btn.setVisible(false);
scr2Btn.setVisible(true);
jSlider.setVisible(true);
sliderLbl1.setVisible(true);
sliderLbl2.setVisible(true);
sliderLbl3.setVisible(true);
}
});
}
return scr1Btn;
}
/**
* This method initializes scr2Btn
*
* @return javax.swing.JButton
*/
private JButton getScr2Btn() {
if (scr2Btn == null) {
scr2Btn = new JButton();
scr2Btn.setBounds(new Rectangle(476, 364, 130, 32));
scr2Btn.setText("Continue");
scr2Btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
connectionSpeedCombo.setVisible(true);
sexCombo.setVisible(true);
itExperienceCombo.setVisible(true);
sectorCombo.setVisible(true);
headlineLbl.setText("Please select the following:");
headlineLbl.setVisible(true);
bodyTxt.setText("Home Connection Speed:\n\nSex: \n\nIT Experience:\n\nSector:");
jSlider.setVisible(false);
sliderLbl1.setVisible(false);
sliderLbl2.setVisible(false);
sliderLbl3.setVisible(false);
scr2Btn.setVisible(false);
startQuiz.setVisible(true);
answer = answer+","+ Math.round(jSlider.getValue()/10);
}
});
}
return scr2Btn;
}
/**
* This method initializes connectionSpeedCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getConnectionSpeedCombo() {
if (connectionSpeedCombo == null) {
connectionSpeedCombo = new JComboBox();
connectionSpeedCombo.addItem("Modem 28.8kb");
connectionSpeedCombo.addItem("Modem 56 kb") ;
connectionSpeedCombo.addItem("ADSL 512kb") ;
connectionSpeedCombo.addItem("ADSL 1mb");
connectionSpeedCombo.addItem("ADSL 2mb") ;
connectionSpeedCombo.addItem("ADSL 8mb") ;
connectionSpeedCombo.addItem("Cable 2mb") ;
connectionSpeedCombo.addItem("cable 5mb");
connectionSpeedCombo.addItem("other");
connectionSpeedCombo.setBounds(new Rectangle(500, 100, 100, 25));
}
return connectionSpeedCombo;
}
/**
* This method initializes sexCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getSexCombo() {
if (sexCombo == null) {
sexCombo = new JComboBox();
sexCombo.addItem("Male");
sexCombo.addItem("Female");
sexCombo.setBounds(new Rectangle(500, 130, 80, 25));
}
return sexCombo;
}
/**
* This method initializes itExperienceCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getItExperienceCombo() {
if (itExperienceCombo == null) {
itExperienceCombo = new JComboBox();
itExperienceCombo.addItem("Novice");
itExperienceCombo.addItem("intermediate");
itExperienceCombo.addItem("expert");
itExperienceCombo.setBounds(new Rectangle(500, 160, 134, 25));
}
return itExperienceCombo;
}
/**
* This method initializes sectorCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getSectorCombo() {
if (sectorCombo == null) {
sectorCombo = new JComboBox();
sectorCombo.addItem("Accountancy");
sectorCombo.addItem("Agriculture");
sectorCombo.addItem("Animals & Environment");
sectorCombo.addItem("Automotive");
sectorCombo.addItem("Banking");
sectorCombo.addItem("Finance & Insurance");
sectorCombo.addItem("Catering");
sectorCombo.addItem("Charity");
sectorCombo.addItem("Construction");
sectorCombo.addItem("Property Maintenance & Surveying");
sectorCombo.addItem("Customer Service & Call Centre");
sectorCombo.addItem("Distribution & Warehouse");
sectorCombo.addItem("Domestic");
sectorCombo.addItem("Driving & Transport");
sectorCombo.addItem("Education");
sectorCombo.addItem("Engineering");
sectorCombo.addItem("Freight and Logistics");
sectorCombo.addItem("Health & Caring");
sectorCombo.addItem("Hospitality");
sectorCombo.addItem("Travel & Leisure");
sectorCombo.addItem("HR & Recruitment");
sectorCombo.addItem("IT");
sectorCombo.addItem("Legal");
sectorCombo.addItem("Manufacturing");
sectorCombo.addItem("Marketing");
sectorCombo.addItem("PR & Advertising");
sectorCombo.addItem("Media/New Media");
sectorCombo.addItem("Public Sector & Social Services");
sectorCombo.addItem("Retail");
sectorCombo.addItem("Sales");
sectorCombo.addItem("Scientific & Pharmaceutical");
sectorCombo.addItem("Secretarial & Administration");
sectorCombo.addItem("Security & Defence");
sectorCombo.addItem("Other");
sectorCombo.setBounds(new Rectangle(500, 190, 190, 25));
}
return sectorCombo;
}
/**
* This method initializes startQuiz
*
* @return javax.swing.JButton
*/
private JButton getStartQuiz() {
if (startQuiz == null) {
startQuiz = new JButton();
startQuiz.setBounds(new Rectangle(500, 260, 153, 32));
startQuiz.setText("Start Quiz >>");
startQuiz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println(connectionSpeedCombo.getSelectedIndex());
int option = connectionSpeedCombo.getSelectedIndex();
switch(option)
{
case 0:
delay = 4200;
break;
case 1:
delay = 3100;
break;
case 2:
delay = 2500;
break;
case 3:
delay = 1800;
break;
case 4:
delay = 800;
break;
case 5:
delay = 400;
break;
case 6:
delay = 1500;
break;
case 7:
delay = 400;
break;
case 8:
delay = 1000;
break;
}
answer = answer +","+connectionSpeedCombo.getSelectedItem()+","+sexCombo.getSelectedItem()+","+itExperienceCombo.getSelectedItem()+","+sectorCombo.getSelectedItem();
connectionSpeedCombo.setVisible(false);
sexCombo.setVisible(false);
itExperienceCombo.setVisible(false);
sectorCombo.setVisible(false);
bodyTxt.setVisible(false);
startQuiz.setVisible(false);
questionSetBackup = new ArrayList<Question>(questionSet);
totalQuestions = questionSet.size();
try{
Thread.sleep(delay);
}
catch(Exception exception){System.out.println(exception);}
if(questionSet.size()==0)
{
gotoQuizfinishScreen();
}
int randomInt = randomGenerator.nextInt(questionSet.size());
currentQuestion = questionSet.remove(randomInt);
choiceA.setVisible(true);
choiceB.setVisible(true);
choiceC.setVisible(true);
choiceD.setVisible(true);
headlineLbl.setText(currentQuestion.getQuestion());
choiceA.setText("A. "+currentQuestion.getChoice1());
choiceB.setText("B. "+currentQuestion.getChoice2());
choiceC.setText("C. "+currentQuestion.getChoice3());
choiceD.setText("D. "+currentQuestion.getChoice4());
}
});
}
return startQuiz;
}
private class ActListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand(); //gets the text from the label
char ans = s.charAt(0);
answer = answer +","+currentQuestion.getQuestion();
if(currentQuestion.Answer == ans)
{
correctAnswers++;
answer = answer + "-Correct";
}
else
answer = answer + "-Incorrect";
System.out.println(correctAnswers);
if(questionSet.size()==0)
{
gotoQuizfinishScreen();
}
else{
try{
Thread.sleep(delay);
}
catch(Exception exception){System.out.println(exception);}
int randomInt = randomGenerator.nextInt(questionSet.size());
currentQuestion = questionSet.remove(randomInt);
choiceA.setVisible(true);
choiceB.setVisible(true);
choiceC.setVisible(true);
choiceD.setVisible(true);
headlineLbl.setText(currentQuestion.getQuestion());
choiceA.setText("A. "+currentQuestion.getChoice1());
choiceB.setText("B. "+currentQuestion.getChoice2());
choiceC.setText("C. "+currentQuestion.getChoice3());
choiceD.setText("D. "+currentQuestion.getChoice4());
}
}
}
public void gotoQuizfinishScreen(){
choiceA.setVisible(false);
choiceB.setVisible(false);
choiceC.setVisible(false);
choiceD.setVisible(false);
headlineLbl.setText("Please Rate the Quiz: ");
bodyTxt.setVisible(true);
bodyTxt.setText("Performance: \n\nDocumentation: \n\nReliability: \n\nUseablity: ");
continueBtn.setVisible(true);
performanceCombo.setVisible(true);
documentationCombo.setVisible(true);
reliabilityCombo.setVisible(true);
usabilityCombo.setVisible(true);
}
/**
* This method initializes choiceA
*
* @return javax.swing.JButton
*/
private JButton getChoiceA() {
if (choiceA == null) {
choiceA = new JButton();
choiceA.setBounds(new Rectangle(200, 150, 160, 40));
choiceA.addActionListener(new ActListener());
choiceA.setBackground(Color.blue);
}
return choiceA;
}
/**
* This method initializes choiceB
*
* @return javax.swing.JButton
*/
private JButton getChoiceB() {
if (choiceB == null) {
choiceB = new JButton();
choiceB.setBounds(new Rectangle(500, 150, 160, 40));
choiceB.addActionListener(new ActListener());
choiceB.setBackground(Color.blue);
}
return choiceB;
}
/**
* This method initializes choiceC
*
* @return javax.swing.JButton
*/
private JButton getChoiceC() {
if (choiceC == null) {
choiceC = new JButton();
choiceC.setBounds(new Rectangle(200, 300, 160, 40));
choiceC.addActionListener(new ActListener());
choiceC.setBackground(Color.blue);
}
return choiceC;
}
/**
* This method initializes choiceD
*
* @return javax.swing.JButton
*/
private JButton getChoiceD() {
if (choiceD == null) {
choiceD = new JButton();
choiceD.setBounds(new Rectangle(500, 300, 160, 40));
choiceD.addActionListener(new ActListener());
choiceD.setBackground(Color.blue);
}
return choiceD;
}
/**
* This method initializes continueBtn
*
* @return javax.swing.JButton
*/
private JButton getContinueBtn() {
if (continueBtn == null) {
continueBtn = new JButton();
continueBtn.setBounds(new Rectangle(500, 290, 160, 35));
continueBtn.setText("Continue>>");
continueBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
bodyTxt.setText("Well Done..\n");
bodyTxt.append("Your Score is "+correctAnswers+" out of "+totalQuestions);
continueBtn.setVisible(false);
performanceCombo.setVisible(false);
documentationCombo.setVisible(false);
reliabilityCombo.setVisible(false);
usabilityCombo.setVisible(false);
continueBtn.setVisible(false);
headlineLbl.setVisible(false);
StartAgain.setVisible(true);
answer = answer + ","+performanceCombo.getSelectedItem()+","+documentationCombo.getSelectedItem()+","+reliabilityCombo.getSelectedItem()+","+usabilityCombo.getSelectedItem()+","+correctAnswers+"";
}
});
}
return continueBtn;
}
/**
* This method initializes performanceCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getPerformanceCombo() {
if (performanceCombo == null) {
performanceCombo = new JComboBox();
for(int i=0;i<10;i++)
performanceCombo.addItem(i+1);
performanceCombo.setBounds(new Rectangle(500, 100, 50, 25));
}
return performanceCombo;
}
/**
* This method initializes documentationCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getDocumentationCombo() {
if (documentationCombo == null) {
documentationCombo = new JComboBox();
for(int i=0;i<10;i++)
documentationCombo.addItem(i+1);
documentationCombo.setBounds(new Rectangle(500, 130, 50, 25));
}
return documentationCombo;
}
/**
* This method initializes reliabilityCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getReliabilityCombo() {
if (reliabilityCombo == null) {
reliabilityCombo = new JComboBox();
for(int i=0;i<10;i++)
reliabilityCombo.addItem(i+1);
reliabilityCombo.setBounds(new Rectangle(500, 160, 50, 25));
}
return reliabilityCombo;
}
/**
* This method initializes usabilityCombo
*
* @return javax.swing.JComboBox
*/
private JComboBox getUsabilityCombo() {
if (usabilityCombo == null) {
usabilityCombo = new JComboBox();
for(int i=0;i<10;i++)
usabilityCombo.addItem(i+1);
usabilityCombo.setBounds(new Rectangle(500, 190, 50, 25));
}
return usabilityCombo;
}
/**
* This method initializes StartAgain
*
* @return javax.swing.JButton
*/
private JButton getStartAgain() {
if (StartAgain == null) {
StartAgain = new JButton();
StartAgain.setBounds(new Rectangle(253, 226, 142, 31));
StartAgain.setText("Start Again");
StartAgain.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
delay = 0;
correctAnswers = 0;
totalQuestions = 0;
scr1Btn.setVisible(true);
questionSet = questionSetBackup;
System.out.println(questionSet.size());
try {
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt", true));
out.write(answer+"");
out.close();
} catch (IOException e1) {
}
gotoScreen1();
}
});
}
return StartAgain;
}
}