Search code examples
javafontsappletmp3access-denied

Applet file access


I have been working on an audio player applet, and I am having a problem with getting it to run in a web browser. It works perfectly in applet viewer in eclipse, but it doesn't work when i try it in Google Chrome. I even tried the generated applet file that eclipse makes, but it doesn't work either, probably something to do with the way eclipse runs it.

this is the html file, but it isn't allowed to access the mp3 files, or the special font i made:

<html>

<meta http-equiv="Content-Type" content="text/html; charset=MacRoman"/>

<body>

<applet code="DOSPlayer.class" archive="DOSPlayer.jar, basicplayer3.0.jar, commons-logging-1.1.1.jar, mp3spi1.9.5.jar, tritonus_share-0.3.6.jar, jl1.0.1.jar" width="450" height="350" >

</applet>

</body>

</html>

The error i am getting is because it can't access the files because of access permissions:

java.security.AccessControlException: access denied (java.io.FilePermission fonts\DOSFont.ttf 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 DOSPlayer.initFont(DOSPlayer.java:398)
    at DOSPlayer.<init>(DOSPlayer.java:413)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    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)
java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission 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.File.list(Unknown Source)
    at java.io.File.listFiles(Unknown Source)
    at DOSPlayer.getFiles(DOSPlayer.java:259)
    at DOSPlayer.<init>(DOSPlayer.java:415)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    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)
Exception: java.lang.reflect.InvocationTargetException

I know that the error is coming from these two snippets of code:

     //path is "mp3/"
public String[] getFiles(String path) {

              String[] songs = new String[501];
              String files;
              File folder = new File(path);
              File[] listOfFiles = folder.listFiles(); 
              //gets all mp3 files within mp3 folder
             try {
              for (int i = 0; i < listOfFiles.length; i++) {

               if (listOfFiles[i].isFile()) 
               {
               files = listOfFiles[i].getName();
                    if (files.endsWith(".mp3")) {
                            songs[songNum] = files;
                            songNum++;
                            playableSongs++;
                    }
                 }
              }
             } catch (Exception e) {
                 System.out.println("Error Here");
                 e.printStackTrace();
                 return null;
             }
              return songs;


}

/////////////////////////////////

public void initFont() {
        try {
            //URL fontUrl = new URL("fonts/DOSFont.ttf");
            File file = new File("fonts/DOSFont.ttf"); // where ever it is from

            FileInputStream fontPath = new FileInputStream(file);
            dosFont = Font.createFont(Font.TRUETYPE_FONT, fontPath);//fontUrl.openStream());
            dosFont = dosFont.deriveFont(Font.PLAIN,19);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(dosFont);
            font = true;
                } catch(Exception e) {
                    e.printStackTrace();
                    font = false;
                }
    }

I have already tried signing the applet, but that doesn't work, it grants access to files on the person viewing the applet from a webpage. I also tried editing the policy file, and i can't get that to work, but it can still be an option.

I have had trouble with this for hours and have no idea what to do, any help would be greatly appreciated.


Solution

  • These resources (especially the font) are effectively 'application resources' that could be added to a Jar and added to the run-time class path of the applet. If not in an archive, they should be added to a public place on the server (where they can be accessed directly from the browser).

    Once in a Jar or on the server, they can be accessed by URL. The URL can be formed as a relative path to the code or document base if not embedded in a Jar. If they are in a Jar, get an URL using something like:

    URL urlToFont = this.getClass().getResource("/path/in/jar/to/the.ttf");
    

    I also tried editing the policy file, and i can't get that to work, but it can still be an option.

    No it can't. Policy files are good for a very limited range of things, and a publicly deployed applet is not one of them.


    And just a quick comment on..

    File[] listOfFiles = folder.listFiles(); 
    

    That will need to be rethought. An applet cannot get a list of files on the server without help from the server. To define a group of files, an applet will typically have a parameter that accepts the paths/names.