1

I wrote a simple Hello world program for RMI. It works well, when the client is in console. I tried using a Swing application as my client, it worked fine even then.

But when I use an applet as the client, it throws the following exception:

Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
        at java.security.AccessController.checkPermission(AccessController.java:546)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
        at java.lang.SecurityManager.checkConnect(SecurityManager.java:1034)
        at java.net.Socket.connect(Socket.java:524)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
        at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
        at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
        at java.rmi.Naming.lookup(Naming.java:84)
        at Client.<init>(Client.java:23)
        at MyApplet.submitActionPerformed(MyApplet.java:179)
        at MyApplet.access$300(MyApplet.java:22)
        at MyApplet$4.actionPerformed(MyApplet.java:84)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Why is that so, and what can I do to make my program work?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
shahensha
  • 2,051
  • 4
  • 29
  • 41

2 Answers2

3

See if this link might give you the information you need to solve this issue:

All applets and any applications invoked with a security manager must be granted explicit permission to access local system resources apart from read access to the directory and its subdirectories where the program is invoked. The Java platform provides permissions to allow various levels of access to different types of local information.

See the above link for more details regarding the different permissions and the policy file.

Julian
  • 20,008
  • 17
  • 77
  • 108
  • 1
    @Shahensha: no, not unless you have defined one yourself. For instance at http://download.oracle.com/javase/tutorial/essential/environment/security.html, you'll see: *Typically, a web applet runs with a security manager provided by the browser or Java Web Start plugin. Other kinds of applications normally run without a security manager, unless the application itself defines one. If no security manager is present, the application has no security policy and acts without restrictions.* – Julian Mar 06 '11 at 10:41
  • RMI checks that you do have a security manager in place (although the fact that there exists a security manager doesn't mean that it is safe). – Tom Hawtin - tackline Mar 08 '11 at 13:25
2

For applets, the simple security rule for network access is: It has permission to connect the remote server it came from, but nothing else.

So, you'll have to run your RMI registry at the same server which is also the webserver on which the applet resides.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • "It has permission to connect the remote server it came from". Correct. Both host and port number must be the same. – Pierre Ernst Aug 23 '11 at 12:36
  • 1
    @Pierre: actually, the port number can be different. I have [an applet](http://www.fencing-game.de/) which is loaded from port 80 (HTTP), but then opens a connection on port 7777 (a custom protocol). And RMI also usually is on another port than HTTP (if you don't have a firewall that blocks the RMI port and must do HTTP-tunneling). – Paŭlo Ebermann Aug 23 '11 at 12:56