Solved

Help with java.net.SocketPermission Error

Posted on 2002-03-21
10
383 Views
Last Modified: 2007-12-19

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!!

Thanks!

dv
0
Comment
Question by:vanfleet
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6886938
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.
0
 

Author Comment

by:vanfleet
ID: 6887490
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.

<HTML>
<HEAD>
  <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
    function callApplet() {
      // Send a text file name to the loadData method
      document.Toc.loadData('filename.txt');

    }

  </SCRIPT>

</HEAD>
<BODY onload="callApplet()" MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0">
<APPLET
  CODE = "Test.class"
  WIDTH = "300"
  HEIGHT = "300"
  NAME = "Toc"
  ALIGN = "left"
  MAYSCRIPT = true>
</APPLET>
</BODY></HTML>

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());
    codeBase=getCodeBase();
 
    // If readData is called from here with a valid file name it reads that file
    // with no problems
    //readData("file.txt");
  }

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

  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);
      }

      br.close();

    } 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);
    readData(datFile);

  }

}

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

Thanks;

dv
 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6888254
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vanfleet
ID: 6888464
CEHU;

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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6888523
Not really I'm afraid, other than looking into possible NS bugs in newsgroups etc.
0
 
LVL 3

Accepted Solution

by:
absong earned 200 total points
ID: 6889865
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.
0
 

Author Comment

by:vanfleet
ID: 6894328
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!

Thanks,

dv  
0
 
LVL 3

Expert Comment

by:absong
ID: 6894965
vanfleet,

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
   process(str);
}

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() {
   document.javaapplet.callURL();
   // 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) {
    process(p_str);
}
0
 

Author Comment

by:vanfleet
ID: 6895051
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,

dv
0
 
LVL 3

Expert Comment

by:absong
ID: 6895320
yeah, sticking to applet is definitely the best idea. if nothing else, it's much faster than javascript in terms of processing.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 50
add projects t working set in maven 2 37
java mysql insert application 14 41
restrict decimal places for double datatype 10 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question