java.security.AccessControlException: access denied (java.io.FilePermission
Hi,
I have written swing code using applets.The code is::
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JButtonDemo extends JApplet implements ActionListener{
JTextField jtf;
public void init(){
Container c = getContentPane();
c.setLayout(new FlowLayout());
ImageIcon ii = new ImageIcon("");
JButton but = new JButton(ii);
but.setActionCommand("myButton");
but.addActionListener(this);
c.add(but);
jtf = new JTextField(10);
c.add(jtf);
}
public void actionPerformed(ActionEvent ae){
jtf.setText(ae.getActionCommand());
}
}
The html is::
<html>
<body>
<applet code="JButtonDemo.class" width="250" height="150">
</applet
>
</body>
</html>
Its getting compiled but when I'm trying to run it like..
appletviewer JButtonDemo.html
its throwing the following error ..
java.security.AccessControlException: access denied (java.io.FilePermission read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
at java.security.AccessController.checkPermission(AccessController.java:401)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
at java.lang.SecurityManager.checkRead(SecurityManager.java:863)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:472)
at sun.awt.SunToolkit.getImage(SunToolkit.java:486)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at JButtonDemo.init(JButtonDemo.java:10)
at sun.applet.AppletPanel.run(AppletPanel.java:353)
at java.lang.Thread.run(Thread.java:534)
could anyone pls help me in rectifying this problem...
Thanks in advance
Srinivas
[1942 byte] By [
srinivas1a] at [2007-10-3 3:16:52]

Have you tried to sign the applet? http://java.sun.com/developer/technicalArticles/Security/Signed/ http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/rsa_signing.htmlSign it.
1, sign it.
2. convert your html page so IE wil not try to use msJVM to run your applet
Signing applets:
http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
second post and reply 18 for the java class file using doprivileged
Still problems?
A Full trace might help us out:
http://forum.java.sun.com/thread.jspa?threadID=656028
How do you convert the HTML so IE won't try to use the MS JVM?
Also, if I successfully sign an applet (self generated certificate using keytool) and when I load the page, the security message comes up asking if I want to trust the applet, AND I click 'Run'
shouldn't it let me have access to the file structure?
and System.getProperty("user.dir"); ?
I'm doing all of that and no matter what I do, I can't gain access to the file structure or the System property.
This is a NIGHTMARE!! How can I make this work?
You must have missed something, here is how I got it to work:
appelt in c:\temp\test.java:import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
public class test extends JApplet implements ActionListener {
JTextField jtf;
public void init() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
ImageIcon ii = new ImageIcon("c:/test.jpg");
JButton but = new JButton(ii);
but.setActionCommand("myButton");
but.addActionListener(this);
c.add(but);
jtf = new JTextField(10);
c.add(jtf);
}
public void actionPerformed(ActionEvent ae) {
jtf.setText(ae.getActionCommand());
}
}
html file in c:\temp:<DIV id="lblOutputText">Output comes here</DIV>
<object
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab"
>
<PARAM NAME = CODE VALUE = test >
<PARAM NAME = ARCHIVE VALUE = sTest.jar >
<param name = "type" value = "application/x-java-applet">
<param name = "scriptable" value = "false">
<comment>
<embed
type = "application/x-java-applet" \
CODE = test \
ARCHIVE = sTest.jar
scriptable = false
pluginspage = "http://java.sun.com/products/plugin/index.html#download">
<noembed>
</noembed>
</embed>
</comment>
</object>
The batch file creating the signed jar in c:\temp\sign.batdel *.cer
del *.com
del *.jar
del *.class
javac test.java
keytool -genkey -keystore harm.com -keyalg rsa -dname "CN=Harm Meijer, OU=Technology, O=org, L=Amsterdam, ST=, C=NL" -alias harm -validity 3600 -keypass password -storepass password
rem keytool -export -alias harm -file exportPublicKey.cer -keystore harm.com -storepass password
jar cf0 test.jar test.class
jarsigner -keystore harm.com -storepass password -keypass password -signedjar sTest.jar test.jar harm
del *.class
pause
harmmeijer:
I copied your code verbatim and tried it.
It WORKED!! Thanks *VERY MUCH* for your help.
I've hunted for 2 days 14-16 hours each, trying to figure out why I couldn't do it.
Thanks for the heads up about dopriviledged. I did see it but have to admit I hadn't tried it. I did try some tests that did not involve Javascript and executed on init, namely Francois Osini's Derby Demo. They didn't work. Yours didn't either until I did *exactly* what you did.
I ended up changing one thing.
When the user clicks the button the textbox is filled with System.getProperty("user.dir"). That also worked and sufficiently proved to me that the Applet has permissions to access the drive system and properties.
So, now I've gotta figure out what you did differently than me.
I noticed that when you jar the class you include: cf0.
I've been using NetBeans 5 to jar it. I'm wondering if that makes a difference.
Anyway, from here I'm *sure* I can make it work.
Thanks again, very much.
Try to put all your commands in a batchfile and then see what's different.
If the origional jar is created by NetBeans or Eclipse that should not be
a problem, check out the batchfile posted before
remove the commands compiling test.java and generating test.jar.
Rename the NetBeans jar to test.jar and try the batchfile on that jar.
It should not be a problem signing that jar.