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 6 of 6

Thread: java.net.URL Access Exception

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy java.net.URL Access Exception

    OMG, I'm so tired of trying to find this answer. I'll do my best to explain the issue. I have compiled, jarred and signed this browser applet:

    import java.applet.Applet;
    import java.net.URL;
    import java.io.*;
     
    public class WebPipe extends Applet {
     
        public String getText(String url)
        throws java.net.MalformedURLException, java.io.IOException
    {
        URL resource = new URL(this.getDocumentBase( ), url);
        InputStream is = resource.openStream( );
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuilder text = new StringBuilder( );
        String line;
        while((line = in.readLine( )) != null) {
            text.append(line);
            text.append("\n");
        }
        in.close( );
        return text.toString( );
    }
    	public void init() {
                // Call f() in HTML page
         }
    }

    In the host page:
    <applet id="WebPipe"
        code="WebPipe.class"
        archive="WebPipe.jar"
        style="width: 1px; height: 1px; float: left;"
        mayscript></applet>
    <input type="button" onClick='alert(document.WebPipe.getText("http://myserver/test.txt"));'>

    What I'm trying to accomplish is event driven data retrieval from a file on my server using the java applet as a "remote data connectivity driver" for JavaScript. I keep getting access denied. I'm not sure if its the browser or the server that is denying access but I can tell you the code works fine when called from the browser's body.onLoad event. I need to be able to post data and retrieve data with this "driver."

    I suppose it really doesn't matter whether it is get or post, as long as I can pass parameters on the requested URL. Here is what the Javascript console reports after clicking the button:
    uncaught exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
    At this point exception handling is unimportant. the inability to get the content of the desired url is my problem.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: java.net.URL Access Exception

    If I understand correctly, you are trying to read a file on your server from an applet. If this is the case applet's are client side - just like javascript - so the server needs to allow the client access. Typically this is done by having a script or listener on the server which serves the requests, using query strings as parameters to vary the response to those requests.

  3. The Following User Says Thank You to copeg For This Useful Post:

    MistaWizurd (April 3rd, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.net.URL Access Exception

    Hmm... Makes no sense to me as to why a typical Webserver (I'm using Apache2 on UBUNTU x64) should deny read access to a public (world readable resource) file. Yet, this is not unfathomable. Thanks for your assistance. I will try writing a script to 'dump' the file in PHP.

  5. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.net.URL Access Exception

    Well, you were absolutely correct about my intent. However using a script, does not solve the issue. Same error using code:
    <?php include("test.txt"); ?>
    All of these files are in the root directory of the server. My apache log file doesn't say anything particulary interesting other than crossdomain.xml not found, but in reproducing the reported client error, the error does not re-manifest itself in the apache log.

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.net.URL Access Exception

    I found a solution to the policy issue. I have not entirely tested this but the adaptation I have created using the classes and methods described here have seemingly solved my policy issues.

    * edit: FALSE POSITIVE see post #6 for the real fix...
    Java HTTP example - Reading and writing to an HTTP server | devdaily.com

    Maybe I was just tired however this is what led me to the code:
    you are trying to read a file on your server from an applet
    Thanks a billion!
    Last edited by MistaWizurd; April 3rd, 2011 at 02:20 AM. Reason: Status update

  7. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.net.URL Access Exception

    Wow, I didn't need to change the code at all. i found a crossdomain.xml file for java and placed it in my virtual host root directory. The crossdomain.xml 404 kept showing up in the access log. So I placed the crossdomain.xml file in the /var/www directory and all the problems have been solved. It is most interesting to note that sometimes, mainly when the code is first loaded into the browser, that the code works fine until the content of the requested file is changed or the browser is reloaded.. Seeing that this problem arose out of security features... i don't feel very secure!

Similar Threads

  1. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  2. no data found ms -access sql exception
    By jatinrai199 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2011, 02:22 PM
  3. Help with java retrieving from Ms Access
    By dever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 10:40 AM
  4. How do I access SENS information in JAVA?
    By cabodge in forum Java Theory & Questions
    Replies: 4
    Last Post: March 9th, 2010, 05:52 PM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 03:11 AM