1

I'm trying to launch my applet locally and this is the exception I get:
I don't know why this is happening the mp3 is already located on the jar file, why can't I access it?

SEVERE: null
java.security.AccessControlException: access denied (java.io.FilePermission file:\C:\applet\CairoNightTrainClient.jar!\Music\train.mp3 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 com.sun.media.codec.audio.mp3.JS_MP3FileReader.getAudioInputStream(JS_MP3FileReader.java:113)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at LogicEngine.DJ.createClip(DJ.java:56)
    at LogicEngine.DJ.<init>(DJ.java:42)
    at GUI.JPanelGameApplet$1.run(JPanelGameApplet.java:65)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • That is very odd. I have a sand-boxed applet that loads an M3U playlist, parses it, then loads MP3s directly off the local file-system (by URL - in sub-directories of where the applet is located) with no problems. That is more suspect than loading an MP3 from a Jar that is on the applet's class-path! But then I cannot quite understand why your code is invoking a `FilePermission` when trying to read from an URL.. – Andrew Thompson Oct 21 '11 at 12:40
  • @AndrewThompson: Hey good you are here, as you see this is what I got after I tried to do what you told me in the post:http://stackoverflow.com/questions/7828708/applet-size-15-megabytes-too-heavy-to-load/7828890#7828890 Anyway tried to use getResourceAsStream method as Tom suggested but then I've managed to bypass the acess exception but gotthe same exception as I wrote in the previous post: – JavaSa Oct 21 '11 at 13:11
  • java.lang.IllegalArgumentException: No line matching interface Clip supporting format MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, unknown frame rate, is supported. at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:459) at LogicEngine.DJ.createClip(DJ.java:65) at LogicEngine.DJ.(DJ.java:47) at GUI.JPanelGameApplet$1.run(JPanelGameApplet.java:65) – JavaSa Oct 21 '11 at 13:11
  • It's because of clip Interface that I use I guess Would be glad to hear about refactoring for my code that will work. Now adding some relevant code(see edits) – JavaSa Oct 21 '11 at 13:13
  • Is there some other library installed here. I notice in the stack trace that the MP3 codec class has line number debug information. Can't remember what the JRE (or OpenJDK) comes with, but I do seem to remember that MP3 is heavily patent laden. – Tom Hawtin - tackline Oct 21 '11 at 13:15
  • Try your current code with an MP3 known to work with the JMF mp3plugin. 3 are available [here](http://pscode.org/media/#sound). (Ignore the links to 'Play in Tracker' - it is quite broken at the moment, as well as being digitally signed.) What is the exact applet element used? What is the structure of the directories where the HTML and Jars are (e.g. are they all in the same directory)? – Andrew Thompson Oct 21 '11 at 13:23
  • I installed the Mp3Plugin and included its jar in my class path I've added relevant code for my previous exception not of the access problem we talked about here because I think I've managed to bypass it. Now having other unsolved exception that talked about in http://stackoverflow.com/questions/7828708/applet-size-15-megabytes-too-heavy-to-load The relevant code for the other exception was added in that post as for some reason I wasn't able to add it here – JavaSa Oct 21 '11 at 13:25
  • @AndrewThompson:Thanks I'll try the 3 music files you gave me and will update you in an hour I guess. Anyway as you asked for all jars(Mp3Plugin and my jar) and HTML in the same folder and I've followed your previous instruction for how to edit the HTML file – JavaSa Oct 21 '11 at 13:36
  • Curious as to you exact code. The file name looks like a jar URL with protocol bit removed (as if file:///) and URL separator replaced by platform file separator. How does it get that from a (filtered) `InputStream`? Looks like it comes from a resource. I guess changed to jar URL connection handling could lead to dodgy code behaving badly. (Oh, and the latest JRE [6u29/7u1, IIRC], right?) – Tom Hawtin - tackline Oct 21 '11 at 13:50
  • @TomHawtin-tackline: my JRE is 1.6.0_26. Can you please repeat other things you wrote I didn't understand you. Thanks – JavaSa Oct 21 '11 at 15:18

1 Answers1

1

Applets (or at least untrusted applets) are not allowed to access the local file system. This is kind of a good thing as most people might get upset if random websites had such access.

If you want to access a file go through javax.jnlp (just google it) and it'll provide an API that presents the user with a file chooser to allow applets to read a specific file of their choosing.

If it's just a file that is part of the applet, use either a resource (Class.getResourceAsStream) or through an http connection (URL)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • I think that this is a bit weird because my jar which is a closed package comes with a text file(or even images!) which I don't get any of this access permissions problems.. Anyway this is the way I access my mp3 files(for example): getClass().getResource("/Music/train.mp3") Therefore I still don't understand where the problem is and what should I do – JavaSa Oct 21 '11 at 12:04