Page 1 of 3 123 LastLast
Results 1 to 20 of 44

Thread: Java security

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default Java security

    I have made an applet that shows pictures and reads from a text file. Is it possible to allow java to do this in an applet.

    Here's the error: java.security.AccessControlException: access denied (java.io.FilePermission . read)

    I don't write to the file or anything just read.
    My IP address is 127.0.0.1

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Yes.
    How are you loading the applet? locally or from a server?
    If locally, the applet should be able to read a local file.
    If from a server, you'll need to give the applet permission to enable it to read a local file.

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Its going on a web page that will obviously be on a server. So what do I need to do to get permission.
    My IP address is 127.0.0.1

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Two ways to give permission to an applet:
    sign the jar file - I've only done that in testing, but it worked.
    Add permissions on the client's computer. This is done with the policytool that comes with java. If there are more than one client, this would be trouble. The policytool program updates the .java.policy file. It hasn't always worked correctly for me.
    Here's a sample from my .java.policy file that allows an applet loaded from E:\Java to read the Tutorial or doc subfolders. (I have an applet I use to search those folders while I'm browsing those files)
    [
    grant codeBase "file:/E:/Java/-" {
    permission java.io.FilePermission "E:/", "read";
    permission java.io.FilePermission "E:/Java/Tutorial/", "read";
    permission java.io.FilePermission "E:/Java/Java-1_6_0/docs/", "read";
    };

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I need to have it so that any random person can view it on the internet. Is their on easy way to do this.
    My IP address is 127.0.0.1

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Why do you need to read a file from the random person's disk? Doesn't sound like something anyone would want to allow.

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I'm not I have a txt file that has a list of words I'm reading in and some pictures I'm displaying that I have to read all this is done on the server but it says when I did it on my computer just making a web page that It couldn't work.
    My IP address is 127.0.0.1

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Your applet can read a file from the server it was loaded from without permissions, no problem. Well maybe just the ordinary programming problems But it can be done.
    it says when I did it on my computer just making a web page that It couldn't work.
    Not sure what that means. Here's an applet I wrote years ago that reads lots of files from its server: Way Points

    Here's the code to read from server:
    Java Code:
            URL u = null;
            BufferedReader in;
    
            if (WayPointDefs.debug) {
                System.out.println("getFile() trying to read: " + fn);
            }
            try {
     
                    // We're in an applet - go to server for the file
    //                u = new URL(getCodeBase() + CGI_DIR + "/GetFile.pl?" + fn);
                    u = new URL(getCodeBase() + fn + (addSfx ==  null ? "" : addSfx));   // do HTTP GET directly
                    URLConnection uc = u.openConnection();
                    in = new BufferedReader(new InputStreamReader(
                                    uc.getInputStream()));
            
                // Read file from "in" and build the lines 
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
    Last edited by Norm; 07-30-2008 at 04:01 PM.

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    When I try to run the applet off my web page it still gives me an error for reading the pictures I can post the code if that's any help.
    My IP address is 127.0.0.1

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Are the files located in the Applet's codebase?
    It(as per the above code) should work if they are.
    Otherwise where are the files you are trying to read?

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    The Files are located in the same folder as the source and the class files. I use ImageIO to read them in so that I can use them.
    My IP address is 127.0.0.1

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Could you post the code where you are trying to read the files using ImageIO?
    Where is the HTML file relative to the applet's class files?

  13. #13
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Untitled Document click on my Applet

    Java Code:
    import java.awt.AlphaComposite;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    
    
    public class PictureGenerator
    {
    	private BufferedImage[] myImages;
    	private Random rand;
    	
    	public PictureGenerator()
    	{
    		rand = new Random();
    		this.setupArray();
    	}
    	
    	private void setupArray() 
    	{
    		File[] tempFiles;
    		tempFiles = (new File(".").listFiles( 
    				new FilenameFilter() 
    				{
    					public boolean accept(File dir, String name) 
    					{
    						return name.endsWith("JPG") || name.endsWith("GIF") 
    							|| name.endsWith("JPEG") || name.endsWith("jpeg");
    					}
    				}));
    		myImages = new BufferedImage[tempFiles.length];
    		for(int i = 0; i < tempFiles.length; i++)
    		{
    			myImages[i] = this.createResizedCopy(tempFiles[i]);
    		}
    		
    	}
    	
    	private BufferedImage createResizedCopy(File aFile)
    	{
    		int imageType = BufferedImage.TYPE_INT_RGB;
    		BufferedImage scaledBI = new BufferedImage(Window.WINDOW_WIDTH, Window.WINDOW_HIEGHT, imageType);
    		Graphics2D g = scaledBI.createGraphics();
    		g.setComposite(AlphaComposite.Src);
    
    		try 
    		{
    			g.drawImage(ImageIO.read(aFile), 0, 0, Window.WINDOW_WIDTH, Window.WINDOW_HIEGHT, null);
    		}
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    
    		g.dispose();
    		return scaledBI;
    	}
    
    	public BufferedImage getNewPicture()
    	{
    		return myImages[rand.nextInt(myImages.length)];
    	}
    	
    }
    My IP address is 127.0.0.1

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    There's some confusion about how you are using your program. I thought the program was an applet that was loaded from a server into a browser on some client's computer. The applet was to read a file from the server to get some info and then use that info to read more files from the server.
    The code you've shown is using the File class which will try to read the file from the local(client's disk). Not permitted. You must use a URL pointing to the applet's codebase to read files from the server.
    tempFiles = (new File(".").listFiles(

  15. #15
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    ok how do I do that so it does the same thing
    My IP address is 127.0.0.1

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    On what system do you want to do it?

  17. #17
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Like Windows is that what you mean. Or Internet Explorer.
    My IP address is 127.0.0.1

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    server or client.

  19. #19
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    The photos and txt file reside on sever side
    My IP address is 127.0.0.1

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    then you'll need code on the server to get the list of files and return it to the client. The applet running on the client can NOT see the contents of folders on the server.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 01-14-2010, 07:45 PM
  2. Replies: 0
    Last Post: 03-31-2008, 04:35 PM
  3. Replies: 4
    Last Post: 08-10-2007, 10:09 PM
  4. java.security.AccessControlException
    By cecily in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 02:49 AM
  5. Replies: 1
    Last Post: 07-23-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •