Applet access denied when loading .obj model

Started by DaleCantwell, July 26, 2011, 12:01:43

Previous topic - Next topic

DaleCantwell

I am having trouble with my applet, this is what I get from the java console. The app works when it's executed from eclipse but when I try run it from html it seems to have a problem with the model. The textures seem to load without a problem. Can anyone help me out with my I'm getting access denied on loading the obj?

Loading texture: res/bg.jpg
Loading texture: res/font.png
Loading texture: res/map.jpg
Loading texture: res/skin.png
Exception in thread "Thread-12" java.security.AccessControlException: access denied (java.io.FilePermission models\scientist\scientistHIGH.obj read)
   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.checkRead(Unknown Source)
   at java.io.FileInputStream.<init>(Unknown Source)
   at java.io.FileInputStream.<init>(Unknown Source)
   at java.io.FileReader.<init>(Unknown Source)
   at org.gamescore.InGameState.init(InGameState.java:135)
   at org.gamescore.GameWindow.initGL(GameWindow.java:151)
   at org.gamescore.GameWindow$1.run(GameWindow.java:49)

kappa

The problem is likely that your obj is now in a jar and your trying to access it using a File/FileInputStream, which can not read from within a jar. Since once its inside a jar its not longer a File (but in a file).

Exception in thread "Thread-12" java.security.AccessControlException: access denied (java.io.FilePermission models\scientist\scientistHIGH.obj read)

You need to fetch it by obtaining an InputStream to it.

broumbroum

are you runing on an UNIX/OSX  platform ?

  • maybe the .obj file is not allowed to be read by everyone (right before you packed it in the .jar !).
  • always ensure you have signed your .jars and that a Policy file is loaded with the involved FilePermission (only if you run from the AppletViewer or a local applet -J-Djava.security.policy=yourPolicyFile).

DaleCantwell

Thanks for the help, I removed the model loading just to make sure that was the cause. Then I got the error below, trying to load a .ogg file. I then remade the jar without loading ogg or obj and it ran perfectly on my site (minus models and sounds of course).
Below is how I load my obj. If I change from bufferedreader to InputStream do you think that would fix it?

BufferedReader br = new BufferedReader(new FileReader("models/scientist/scientistHIGH.obj"));		
playerModel= new MyObjLoader(br,true);


I am running on Windows in response to the UNIX/OSX question. I don't know much about signing jars so I'll give that a look, maybe that will fix the .ogg issue.

Loading: res/footstep.ogg
java.security.AccessControlException: access denied (java.lang.RuntimePermission getClassLoader)
   at java.security.AccessControlContext.checkPermission(Unknown Source)
   at java.security.AccessController.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkPermission(Unknown Source)
   at java.lang.Thread.getContextClassLoader(Unknown Source)
   at org.gamescore.sound.SoundLoader.getOgg(SoundLoader.java:140)
   at org.gamescore.InGameState.init(InGameState.java:133)
   at org.gamescore.GameWindow.initGL(GameWindow.java:151)
   at org.gamescore.GameWindow$1.run(GameWindow.java:49)

kappa

Quote from: DaleCantwell on July 26, 2011, 20:45:37
BufferedReader br = new BufferedReader(new FileReader("models/scientist/scientistHIGH.obj"));		
playerModel= new MyObjLoader(br,true);

...
Below is how I load my obj. If I change from bufferedreader to InputStream do you think that would fix it?
As mentioned in my post above, your using File (FileReader in this case) so you can't read files from within a jar using that.

What you need to do is change FileReader to InputStreamReader and supply InputStreamReader with an InputStream from the jar, this inputstream could for example be got by doing:

Thread.currentThread().getContextLoader().getResourcesAsStream("models/scientist/scientistHIGH.obj");

or

YourClassName.class.getResourcesAsStream("models/scientist/scientistHIGH.obj");

DaleCantwell

Thanks again for the help, the inputstream fixed the issue however I get this error now

Exception in thread "Thread-12" java.security.AccessControlException: access denied (java.lang.RuntimePermission getClassLoader)

I think this is because I don't have permissions.

kappa

probably means it can't get the context loader. try

getClass().getResourcesAsStream("models/scientist/scientistHIGH.obj");

DaleCantwell

If I use
InputStream is = Thread.currentThread().getClass().getResourceAsStream("models/scientist/scientistHIGH.obj");

or

InputStream is = InGameState.class.getResourceAsStream("models/scientist/scientistHIGH.obj");

I get the error below, the applet wont even run locally from eclipse, could it be that if I use the original method and allow some siging or grant permission it may work? I don't really know much on signing applets so I'm not sure.

Exception in thread "Thread-4" java.lang.NullPointerException
   at java.io.Reader.<init>(Unknown Source)
   at java.io.InputStreamReader.<init>(Unknown Source)
   at org.gamescore.InGameState.init(InGameState.java:129)
   at org.gamescore.GameWindow.initGL(GameWindow.java:151)
   at org.gamescore.GameWindow$1.run(GameWindow.java:49)

kappa

when running the applet locally, make sure files are in a jar and added to the classpath. You shouldn't really need to sign the applet jars yourself for a task such as reading files from a jar.

broumbroum


DaleCantwell

Quote from: kappa on July 27, 2011, 11:08:21
when running the applet locally, make sure files are in a jar and added to the classpath. You shouldn't really need to sign the applet jars yourself for a task such as reading files from a jar.

Is there anything in particular I need to do to ensure the .obj is in the jar and classpath? I have it in the same location as my images and they seem to load fine so I jsut assumed the .obj was being included too.

Quote from: broumbroum on July 27, 2011, 19:28:32
Please sign your applet .jars..

What is the best free method to do this and is there any tutorials you can reccomend? This is part of an academic project so I will need to have the applet available online for my supervisors but I wont be able to pay for any official signatures.

Thanks for any help.

kappa

Quote from: DaleCantwell on July 28, 2011, 10:19:55
Is there anything in particular I need to do to ensure the .obj is in the jar and classpath? I have it in the same location as my images and they seem to load fine so I jsut assumed the .obj was being included too.
Your image loader code (if its Slick-Util) is probably more robust and tries both InputStream and File methods internally. To put the .obj's on the classpath, simply zip the 'models' folder and add it to the classpath (same place you put lwjgl.jar). Then try the above mentioned method using getResourceAsStream thing. These steps are only needed to test locally in eclipse (since they'll already be in a jar and on the classpath when run in the browser).

Quote from: DaleCantwell on July 28, 2011, 10:19:55
What is the best free method to do this and is there any tutorials you can reccomend? This is part of an academic project so I will need to have the applet available online for my supervisors but I wont be able to pay for any official signatures.
Although signing will get you past the access denied part, I'm not sure it'll help in this case because the actual resource isn't being found.

DaleCantwell

I changed the code to whats in the codeblock below and now it loads with no issues. I had moved the location of the models folder from where the .project is located to my src location just before this but I don't know if that had anything to do with it. Thanks for all the help hopfully this will be usful to anyone else who gets a similar issue.

        InputStreamReader isr = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("models/scientist/scientistHIGH.obj"));
	BufferedReader br = new BufferedReader(isr);

broumbroum

Quote from: DaleCantwell on July 28, 2011, 10:19:55

What is the best free method to do this and is there any tutorials you can reccomend? This is part of an academic project so I will need to have the applet available online for my supervisors but I wont be able to pay for any official signatures.

Thanks for any help.

browse to your JDK folder -> /bin/jarsigner(.exe) , using a terminal app. (use start -> search -> "cmd" to launch the command line tool on windows). run it with no args and you should understand how to sign. something like jarsigner -genkey alias etc. then jarsigner -sign jarfile signedjarfile
you can sign your .jar with a keypair that you keep private (online, your applet will be given all security privileges IF AND ONLY IF the user accepts the certificate). if you want it public, then buy a Verisign / thawte etc. , a code signing certificate (really expensive).
It's your academic work, isn't it?  then you may find a code signing certificate at no cost through your academy... Ask your supervisors about that !

jediTofu

For reading files inside the jar, you don't need to sign your jars, as has already been mentioned, only if you're reading files from the user's file system.
And actually, you can read from the file system, but only from the directory in which your applet is deployed.

http://download.oracle.com/javase/tutorial/deployment/applet/data.html
http://download.oracle.com/javase/tutorial/deployment/applet/security.html

When running your Applet locally however, you have permissions for everything, so the code is almost always unrelated to signing, unless your browser is super strict or your Java settings deny it.

LWJGL has signed jars (in the applet zip download) with a valid cert, and you can deploy your unsigned jar with the signed jars.  That way people only see one cert (and is legit), which is really the whole purpose of LWJGL going to all that trouble to get a cert and sign the jars.  If you couldn't read files from within your unsigned jars, that cert LWJGL got would be a complete waste really; what apps don't use images nowadays?  So just get a cert if you plan on reading/writing to the file system, messing with hardware (other than what LWJGL already does), reading specific system properties, and any other stuff listed in link above.
cool story, bro