0

I have a problem that started occuring after I updated Java (I think) When I try and connect to my Cpp server (that seems to work just fine) I get an error message I can't find any help with.

Below are the error messages

java.security.AccessControlException: access denied
("java.net.SocketPermission" "127.0.0.1:4000" "connect,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
    at java.security.AccessController.checkPermission(AccessController.java:555)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
    at java.net.Socket.connect(Socket.java:574)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at jclientbare.init(jclientbare.java:27)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:722)

The Java source code follows

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class jclientbare extends Applet {


    static BufferedReader in;
    static PrintStream out;

    public void init()  {

         try    {
                System.out.println("Test NN");
            Socket socket = new Socket( "localhost", 4000 );
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintStream( socket.getOutputStream(), true);
         }

         catch (UnknownHostException e) {

        JOptionPane.showMessageDialog(null, "Unknown Host..");
            System.out.println("Unknown host: kq6py");

           } catch  (IOException e) {

        JOptionPane.showMessageDialog(null, "NO IO.");
            System.out.println("No I/O");
         }
     }

}

The Java applet doesn't register a connection it just bombs with the error message. Any help would be appreciated! I think it may be a issue with the java.policy but I don't know exactly how to fix it.

Uhh, how do you do a stack trace?

Confident
  • 321
  • 4
  • 17

3 Answers3

2

Try this to give permissions:

grant 
{
  permission java.net.SocketPermission 
  "127.0.0.1:4000", "connect,resolve";
};

For detailed information about granting permissions, you can check the following link : http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html

The article explains nicely where the policy file(s) are located and how to run an application with specific policy file.

The following link details where to find the policy files and the order in which the policy files are loaded : http://download.java.net/jdk8/docs/technotes/guides/security/PolicyFiles.html#DefaultLocs

As far as I understand, if you are in windows, then you can place a file '.java.policy' in your 'my documents' folder. When you run an applet in your browser, it will find this policy file as the user policy file.

sandyiscool
  • 561
  • 2
  • 9
  • which policy file do I add this text to? – Confident Sep 10 '12 at 04:17
  • how do I add the extra java parameters to an applet to be launched from a browser if I wanted a specific policy file – Confident Sep 10 '12 at 04:20
  • Do I need to wrap the socket code in a DoPriviliged? This jar file will be public and I think signing the jar file (if it will work) will be the best solution. BTW this jar file will be connecting to a Cpp server on the server-side – Confident Sep 10 '12 at 04:59
  • Thanks for your help but this still doesn't work o_O If I jar the file will that work... I am dealing with JRE v7 – Confident Sep 10 '12 at 05:40
2
System.exit(1);

Even a trusted applet cannot call System.exit(int). An applet with no security manager should not call for the VM to end. It is like the guest burning down the guest house.

It would be better to do something like:

URL crash = new URL(getCodeBase(), "crash.html");
getAppletContext().showDocument(crash);

And ignore the stuff about policy files. They do not solve anything for real world (wild web) deployment.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your responce. My IDE says the problem is with "Socket socket = new Socket( addr, 4000 );" If I sign the jar would that be the best course of action or should I do something else? I tried it but I could continue pursuing that – Confident Sep 10 '12 at 05:57
  • Sorry about the formatting but I removed the System.exit lines and I still get the same error. And no I don't see any dialogs just that same error – Confident Sep 10 '12 at 06:56
  • I appreciate your consideration... I'm kinda at a loss here – Confident Sep 10 '12 at 06:59
  • Are you flushing the Java Cache using the Java Console between each run? Make sure you do. Add `System.out.println("Test NN");` to the run of the latest (logically formatted) code, get the output/stack-trace & code & add them as an edit to the question. – Andrew Thompson Sep 10 '12 at 07:11
0

Aha! I learned you cannot run a applet on the same source that sent you the class files. After installing Lighttpd I can get to my applet with http://localhost/index.htm. Hope this will help others!

Confident
  • 321
  • 4
  • 17