• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Help with java.net.SocketPermission Error

I have an applet that's been working for a long time in Netscape 4.7x and IE, but it will not work in Netscape 6.2.1. This applet reads in an XML data file as a URL from the same machine that the applet is on. The line that is giving me the problem is:

    BufferedReader br = new BufferedReader(new InputStreamReader(tocUrl.openStream()));

Where tocUrl is a valid URL to my XML document, underneath document root.

The error message I get with Netscape 6.2.1 is:

    IO Error in Toc.readData: java.security.AccessControlException: access denied (java.net.SocketPermission cypci654 resolve)
    java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException: java.lang.NullPointerException
     at Toc.getPramVal(Toc.java:190)
     at TocData.getData(Toc.java:529)
     at Toc.loadData(Toc.java:143)
     at java.lang.reflect.Method.invoke(Native Method)
     at sun.plugin.liveconnect.PrivilegedCallMethodAction.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)

I understand that an applet can not read an external file unless that file is on the same server as the applet, which is the case here. Can someone tell me how to make this work!!


  • 4
  • 3
  • 2
  • +1
1 Solution
You might check and make sure that when you open the URL back to the server, you use getCodeBase() instead of passing the url any other way.  Different security managers can be fickle about this.
vanfleetAuthor Commented:
Thanks for your input. Here is what I've learned so far about this problem.

I setup a separate test area to look at this, and I was able to read in the data from the XML file without any problems using NS 6. So, I started looking at the difference between my test area  (where I got this to work), and my original applet (where I can't get it to work).

I discovered that the only difference between the two, is that in my original applet, the Java method that is reading the XML file is called from the browser with a line of JavaScript. When I tried this same thing in my test area I was able to recreate the original error message.

Here is all the code from my test area so you can try this yourself:

This is the test.html file that loads the applet and called the loadData method
in the applet.

  <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
    function callApplet() {
      // Send a text file name to the loadData method



  CODE = "Test.class"
  WIDTH = "300"
  HEIGHT = "300"
  NAME = "Toc"
  ALIGN = "left"
  MAYSCRIPT = true>

This is the Test.java file for the applet:

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.Applet;


public class Test extends Applet {
  static URL codeBase;

  public void init() {
    System.out.println("Code Base: " + getCodeBase());
    // If readData is called from here with a valid file name it reads that file
    // with no problems


  public static void readData(String dataFile) {

    URL tocUrl=null;
    String str;

    try {
      tocUrl = new URL(codeBase + dataFile);

    } catch (MalformedURLException e) {
      System.out.println("URL Exception :" + e);


    System.out.println("tocUrl: " + tocUrl);

    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(tocUrl.openStream()));
      while((str=br.readLine())!=null) {
        System.out.println("str: " + str);


    } catch (Exception e) {
      System.out.println("Exception: " + e);



  // When loadData is called from JavaScript access
  // a denied error is displayed.
  public void loadData(String datFile) {
    System.out.println("loadData method called: " + datFile);



Any Help that anyone can give me on this would be greatly appreciated.


Just a stab in the dark, but try in the JavaScript function calling not document.Toc.loadData('filename.txt'); but document.Toc.readData('filename.txt');

I assume the path to the file is not showing as mangled in any of your debugging println() calls?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

vanfleetAuthor Commented:

If I try calling the readData method rather than loadData I have a problem because readData is a static method and can't be called from JavaScript. However, when I changed readData from a static method to a regular method, then called it from the JavaScript, I got the same error message in NS 6.2.1:

Exception: java.security.AccessControlException: access denied (java.net.SocketPermission cypci654 resolve)

Any other ideas?
Not really I'm afraid, other than looking into possible NS bugs in newsgroups etc.
i know a way to solve this problem, but it involves callback. according to http://www.kfki.hu/~cspeter/jvm-bugs/ns6jsace.html :

Netscape does not let applets open http and file connection in methods called from JavaScript. The I/O connections should be opened in a separate thread that is created before the JavaScript calls, in applet init().

the site also provides sample code to avoid this problem. hope this solves your problem. i have a similar problem, but callback is not an option.
vanfleetAuthor Commented:
Absong, this is a good workaround, and I think it will work for me. I didn't quite understand what you meant when you said that it involves "callback". Could you explain this to me, I'm not that experienced with using threads!



first of all, i reread your problem again today, and i am not so sure if the solution i posted may help you. your problem sounds very familiar, but i don't really know if it is the same as mine. having said that ...

when I mentioned callback I was actually referring to a similar problem I am having. I can explain briefly:

like you, I am trying to call a Java function from within a JavaScript function, but I need to wait for the Java function to return to process the result. Like the code below:

// jsfun() is a javascript function
function jsfun() {
   // javaapplet is a Java applet
   var str = document.javaapplet.callURL();
   // upon return, str needs to contain the requested data
   // for me to process

the above code would fail, because callURL() will not return with requested data until jsfun() has completed execution, but jsfun() will not complete execution until callURL() returns data.
the way around this problem is, don't process the requested data within jsfun(), instead, make callURL() call another javascript jsfun2() function to process the data after the data is retrieved:

function jsfun() {
   // callURL() immediately returns without waiting for
   // the request to complete. this allows jsfun() to
   // immediately return too.

// after callURL() obtains the data from server, it calls
// another javascript function jsfun2(), that processes
// this data
function jsfun2(p_str) {
vanfleetAuthor Commented:
You are doing someing similar to me, I think, except you're having the Java method call back to a JavaScript function when the processing is done. I'm haveing the Java thread call back to the applet when it's done.

I think the solution you posted will work well for me. Thanks for your help,

yeah, sticking to applet is definitely the best idea. if nothing else, it's much faster than javascript in terms of processing.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now