Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JApplet Local File Permission

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default JApplet Local File Permission

    So I'm building an applet that will run exclusively on my machine, instead of the internet (I have my reasons for using an applet instead of an application), and I am running into those pain-in-the-ass permission issues with writing to text files from an applet.

    I've been googling for the past hour and based on what I've found, I need to create a policy file to allow permission for the applet to write to the local txt files. The problem with this is that I'm not entirely sure how to correctly make a policy file and I'm not sure how to get the applet to use the policy file. I've been reading tutorials and other forums, but nothing I've tried has made any difference. Can anyone give me some guidance here?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: JApplet Local File Permission

    Here are some lines from my .java.policy file. Located at:
    C:\Documents and Settings\Owner\.java.policy


    grant codeBase "file:/D:/JavaDevelopment/Testing/WriteFile/-" {
    permission java.io.FilePermission "D:/Testing/*", "read, write";
    };

    here is the code that writes to a file. I copied it from a textbook ten years ago but can't remember which one
     
    /**
      * By default, this applet raises a security exception, unless
      *  you configure your policy to allow applets from its location
      *  to write to the file "writetest".
     
      <applet code=WriteFile.class width=700 height=150>
      </applet>  
    */
     
    import java.awt.*;
    import java.io.*;
    //import java.lang.*;
    import java.applet.*;
    import java.util.Date;
    import java.security.*;
     
    public class WriteFile extends Applet {
        String myFile = "D:\\Testing\\writetest.txt";    // The file to write
        File f = new File(myFile);
        DataOutputStream dos;
     
        String message = "";
     
      public void init() {
        String osname = System.getProperty("os.name");
        System.out.println("os.name is " + osname);
        System.out.println("Java version is " + System.getProperty("java.version"));
        // Following added for tests with IE7 - Still does not allow wildcards in .java.policy file
    /*
    grant codeBase "file:/D:/JavaDevelopment/Testing/WriteFile/-" {
    /*  permission java.io.FilePermission "D:/Testing/writetest.txt", "read, write";  IE7 works*/ /*
      permission java.io.FilePermission "D:/Testing/*", "read, write";   // IE7 fails
    };
    */
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                System.out.println("Starting PrivilegedAction");
             	try {
               	  dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128));
             	  dos.writeBytes("Cats can hypnotize you when you least expect it\n" + new Date() + "\n");
             	  dos.flush();
                  dos.close();
             	  message = "Successfully wrote to the file named " + myFile 
                                      + " -- go take a look at it!";
             	}
             	catch (SecurityException e) {
             	  message = "writeFile: ex: " + e;
                  e.printStackTrace();
                }
             	catch (IOException ioe) {
             		message = "writeFile: caught i/o exception " + ioe;
                  ioe.printStackTrace();
                }
                repaint();              // go show message
                return null;
            }
        });
      } // end init()
     
     public void paint(Graphics g) {
       g.drawString(message, 10, 10); // show the message
    /*
    	try {
      	  dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128));
    	  dos.writeBytes("Cats can hypnotize you when you least expect it\n" + new Date() + "\n");
    	  dos.flush();
    	  g.drawString("Successfully wrote to the file named " + myFile + " -- go take a look at it!", 10, 10);
    	}
    	catch (SecurityException e) {
    	  g.drawString("writeFile: ex: " + e, 10, 10);
       }
    	catch (IOException ioe) {
    		g.drawString("writeFile: caught i/o exception", 10, 10);
       }
    */
     }
    }

    It's executed from an HTML file located at:
    D:\JavaDevelopment\Testing\WriteFile\WriteFileFmCl ass.html
    Last edited by Norm; July 27th, 2011 at 12:22 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: JApplet Local File Permission

    Alternative you could sign the applet
    OTN Discussion Forums : How to sign an applet (and get it to ...

    The process might seem intensive, but once the certificate is created you can reuse it (I use an ant build script to automate the build process and sign the resulting jar)

Similar Threads

  1. Replies: 1
    Last Post: April 21st, 2011, 09:59 AM
  2. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 03:49 PM
  3. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM
  4. Replies: 0
    Last Post: March 5th, 2010, 12:32 AM
  5. recursive search of all local disks
    By ttsdinesh in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 08:23 AM