Hello,

I am writing a Java applet which uses the JExcel API to write to an Excel file. I have pasted the run-time error that I get below. After some research about the Exception, I added a policy entry using the policytool command for any Java program by selecting CodeBase<ALL>. That did not work. I would be appreciate any help on how to fix this issue. All my program files and the "file.xls" is stored in the same folder.

Thank you!

import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.awt.Color;
import java.io.IOException;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.LineNumberReader;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.write.Number;
import jxl.write.*;
import jxl.read.biff.BiffException;
import jxl.Workbook;

public class GUI extends JApplet
{

    private WritableWorkbook workbook;         
    private WritableSheet sheet[] = new WritableSheet[10]; 

    public void init()
    {
        initializeSheets();
        setVisible (true);
        setSize (500, 500);
    }

    public void initializeSheets()
    {
       try {
            String fileName = "file.xls";
            try{
                       workbook = Workbook.createWorkbook(new File(fileName));
               }
            catch (IOException e){}
            catch  (ExceptionInInitializerError er){}

            for(int i = 0; i < 10; i++)
            {
                workbook.createSheet("Sheet" + i , i);
                WritableSheet sheet = workbook.getSheet(i);
            }           

            Number n = new Number(0,0,0);
            Number num = new Number(0,0,1);
            sheet[0].addCell(n);
            sheet[1].addCell(num);
            try{
                workbook.write();
                workbook.close();
               }
                catch (IOException e){}
          }
          catch (WriteException e) { }
        setSize(500,500);
        setVisible (true);
    }


}
java.security.AccessControlException: access denied ("java.io.FilePermission" "
:\Users\sridhar\Desktop\jexcelapi\jxl.jar" "read")
       at java.security.AccessControlContext.checkPermission(AccessControlCont
t.java:366)
       at java.security.AccessController.checkPermission(AccessController.java
55)
       at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
       at java.net.URLClassLoader$4.run(URLClassLoader.java:690)
       at java.net.URLClassLoader$4.run(URLClassLoader.java:688)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.getPermissions(URLClassLoader.java:688)
       at sun.applet.AppletClassLoader.getPermissions(AppletClassLoader.java:2
)
       at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoade
java:206)
       at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:1
)
       at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
       at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:180)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
       at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
       at java.lang.Class.getDeclaredConstructors0(Native Method)
       at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
       at java.lang.Class.getConstructor0(Class.java:2714)
       at java.lang.Class.newInstance0(Class.java:343)
       at java.lang.Class.newInstance(Class.java:325)
       at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
       at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
       at sun.applet.AppletPanel.run(AppletPanel.java:378)
       at java.lang.Thread.run(Thread.java:722)

Can you post the contents of the .java.policy file and its location?
The policytool should have found it in the correct location, but it can be overridden to write to a location that the java program does not look at. Mine is located at:

C:\Users\Norm\.java.policy

I think its found by the user.home property.

Edited 4 Years Ago by NormR1

Hello,

Thank you for the reply. It will help me a lot if you could tell me how do I go about setting the read permissions for all external files for my applet given what I have in my java.policy file below.

Location of the java.policy file:
C:\Program Files (x86)\Java\jre7\lib\security


Contents of the java.policy file:

// Standard extensions get all permissions by default

grant codeBase "file:${{java.ext.dirs}}/*" {
    permission java.security.AllPermission;
};

// default permissions granted to all domains

grant { 
    // Allows any thread to stop itself using the java.lang.Thread.stop()
    // method that takes no argument.
    // Note that this permission is granted by default only to remain
    // backwards compatible.
    // It is strongly recommended that you either remove this permission
    // from this policy file or further restrict it to code sources
    // that you specify, because Thread.stop() is potentially unsafe.
    // See the API specification of java.lang.Thread.stop() for more 
        // information.
    permission java.lang.RuntimePermission "stopThread";

    // allows anyone to listen on un-privileged ports
    permission java.net.SocketPermission "localhost:1024-", "listen";

    // "standard" properies that can be read by anyone

    permission java.util.PropertyPermission "java.version", "read";
    permission java.util.PropertyPermission "java.vendor", "read";
    permission java.util.PropertyPermission "java.vendor.url", "read";
    permission java.util.PropertyPermission "java.class.version", "read";
    permission java.util.PropertyPermission "os.name", "read";
    permission java.util.PropertyPermission "os.version", "read";
    permission java.util.PropertyPermission "os.arch", "read";
    permission java.util.PropertyPermission "file.separator", "read";
    permission java.util.PropertyPermission "path.separator", "read";
    permission java.util.PropertyPermission "line.separator", "read";

    permission java.util.PropertyPermission "java.specification.version", "read";
    permission java.util.PropertyPermission "java.specification.vendor", "read";
    permission java.util.PropertyPermission "java.specification.name", "read";

    permission java.util.PropertyPermission "java.vm.specification.version", "read";
    permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
    permission java.util.PropertyPermission "java.vm.specification.name", "read";
    permission java.util.PropertyPermission "java.vm.version", "read";
    permission java.util.PropertyPermission "java.vm.vendor", "read";
    permission java.util.PropertyPermission "java.vm.name", "read";
};

That location is different from mine. I'm not sure if the file in that location is the one used by by the java program. Can you explain how it got to that location?
Also you say the filename is java.policy NOT .java.policy

The permission to allow all files to be accessed:
permission java.io.FilePermission "<<ALL FILES>>", "read,write,execute";

Hello,

Thanks for your reply and patience!

I downloaded the SDK and JRE to my C:\ drive. I found the java.policy (I could find any .java.policy folder) document in the lib\security folder.

Using the policyoption command, I specified the FilePermission "<<ALL FILES>>", "read,write,execute"; settings. But, I still get the same error as mentioned in the first message.

Is there a peice of code I can add in my program which overrides any default permissions that have been set in the SDK/JRE?

Thank you!!

I could find any .java.policy folder

The file's name is .java.policy (its a file not a folder) and is located in the user.home folder. Use the policytool to create it and save it in that folder.
Don't change any of the files in the JDK or JRE's folders.

If you could write code to override permissions, then permissions would be meaningless.

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