PDA

View Full Version : RuntimePermission accessDeclaredMembers


ld_pvl
07-03-2009, 11:45 PM
How can I gain above permission for my Applet?

I've been working on this GUI application. One of the functions of this application is to read some values from a file and from those values generate the content for some tables, the content of the tables is calculated via a class called BlackScholes which uses common-maths library. For the sake of simplicity, in the beginning, I hardwired the path (URL) leading to that file, which is basically the URL of my host. Therefore it would not work on any other locations except the host specified.

In the beginning I decided to write the GUI seperately (for some reasons), then just create one of its object in an applet. Like below:

import fdrs.FX.*;

import javax.swing.*;

public class MainApplet extends JApplet {

public void init() {

Main main = new Main();
main.setVisible(true);

}

}



So far so good, I've been uploading the JApplet-Gui application occasionally onto the host and it runs fine with no problems or incoherences.

Then I decided to "softwire" the path (URL) directing the application to the file, so that the application could run anywhere without throwing me the Security related errors. So I rewrote the applet code as follows:

import fdrs.FX.*;

import java.net.*;
import javax.swing.*;

public class MainApplet extends JApplet {

URL codeBaseURL;

public void init() {

codeBaseURL = this.getCodeBase();
Main main = new Main(codeBaseURL);
main.setVisible(true);

}

}



The application indeed could run anywhere, i tested on different servers. However, the application only partially works correctly, as for some reason it complains about some kind of error from the apache maths library.

This is very strange because I did not change anything besides the code above, and just one line (instead of hardwiring now I use the codeBaseURL parameter) in the class which uses the URL path to read from the file.

This is the error that it throws:

Exception in thread "AWT-EventQueue-1" java.lang.ExceptionInInitializerError
at org.apache.commons.math.special.Gamma.regularizedGammaP(Gamma.java:180)
at org.apache.commons.math.special.Erf.erf(Erf.java:56)
at org.apache.commons.math.distribution.NormalDistributionImpl.cumulativeProbability(NormalDistribution Impl.java:110)
at fdrs.FX.BlackScholes.ND1(BlackScholes.java:53)
at fdrs.FX.BlackScholes.callPrice(BlackScholes.java:77)
at fdrs.FX.Table.UpdateSpotTable(Table.java:42)
at fdrs.FX.Main.UpdateTables(Main.java:112)
at fdrs.FX.Main.MaturityTextFieldKeyReleased(Main.java:1034)
at fdrs.FX.Main.access$700(Main.java:12)
at fdrs.FX.Main$8.keyReleased(Main.java:458)
at java.awt.Component.processKeyEvent(Component.java:6095)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2799)
at java.awt.Component.processEvent(Component.java:5911)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4373)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission accessDeclaredMembers)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkMemberAccess(SecurityManager.java:1662)
at java.lang.Class.checkMemberAccess(Class.java:2157)
at java.lang.Class.getDeclaredMethod(Class.java:1934)
at org.apache.commons.math.MathException.<clinit>(MathException.java:49)

Please help me on this matter, thanks in advance
LD

ckeyrouz
07-04-2009, 06:29 AM
Applets are a bit tricky to trifle with unless you know what you are doing.
Applets contain lots of security issue and one of them is not allowing the Applet to access any file outside its context which means Applets are not allowed to access any file from the Operating System or any physical file on the hard drive.

But there is a solution for it.
under your jre/security folder there are 3 files:
java.policy
java.security
javaws.policy

check them and in the last file you can add some security rules but this is not very healthy since any change in the java policy might let other applets access system files.

The best solution is to generate a certificate key and sign the applet with it, just go to the following url and you will find what you need there:

http://www.captain.at/programming/java/

Hope it will be helpful and keep us posted with the result.
Thanks

ld_pvl
07-04-2009, 06:51 AM
Applets are a bit tricky to trifle with unless you know what you are doing.
...
Hope it will be helpful and keep us posted with the result.
Thanks

Actually, my application does not need to read from a file which is located on any hard-drive.

The only file that the application needs to read is located on the same server where the applet class is hosted as well.

The application runs, and it reads from the file without any errors.

Let me be a little more specific.

I don't know if this is the problem with the fact that I "softwired" the URL path. I mentioned the URL because that's the only change I made before the error occurred. The application starts up, reads the file from the URL path, passes the values and it's done, the application never touches this function again. And this function is carried out in a class called DefaultValues which is not in the error report.

What I do that actually throws the error is when I change the values in the JTextFields and let the application calculate something. This function is carried out in the BlackScholes class and this class is actually mentioned in the error report.

Hope this extra information is useful.

And btw, I checked out your link, it's great =] and also a question on top of that. How can I give RuntimePermission: accessDeclaredMembers to the applet?