This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have created an RMI server and an applet that accesses the server.
When I run the applet from Netbeans, everything works great and I get the response back from the server. I have also created a non-applet client and that seems to work as well.
Even though the code was developed using Netbeans, I didn't use any of the GUI tools.
If I run it from my browser (either FireFox or IE), the applet loads, but when I push the button that calls the RMI method I get the following exception:
I have found lots of topics and tips on this, but can't really seem to get anything to work.
This is the first RMI server / applet combination I have written, so bear with me if it's an obvious problem.
I have verified that the server is running using the netstat command.
I am running Java version 1.6.0_13.
Also, I'm just trying to get this to work, that's why the getLargestPrime method is really rather useless....
I'm thinking SocketPermission exception indicates that the socket the client is attempting to connect to and the socket the server is listening on are different. I started the rmiregistry explicitly like 'start rmiregistry 1099' just to make sure it is using the expected port. After I started the RMI Server, I ran netstat and found this line 'TCP 192.168.0.100:1099 myPC:49695 ESTABLISHED'. The IP address here corresponds to my PCs IP address.
Using the appletviewer, I'm getting the same Exception as I'm getting from the browsers. Here is the Stacktrace:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
Am I correct in my understanding that the RMI Server will use the same port as the RMI Registry?
As far as using a security manager, I didn't have much luck, however it could have been that I didn't fully understand how to use it. Also, I thought that the security manager was for applications and not for applets?
I was just reading some documentation that talked about hosting both the applet, server and html on a webserver. Do I need to be doing this? (Very new to all of this!) I realize if I want the world to see it, it needs to be hosted, but I just need it to work locally.
Are you running this on Windows or Unix? On windows if your IP address is say 192.168.0.100 then the server log should say "192.168.0.100:1099" but I it said "127.0.0.1:1099". Only unix can produce 127.0.0.1 without changing the networking hosts file.
Also how are you binding your remote object? I'm not sure if applet binds with RMI different from application or not because your applet code does not have some sort of lookup code for the RMI remote object that applications do.
For your server main method, try adding the createRegistry line and specifying the rmi protocol:
Joined: Aug 08, 2006
I have made the code changes you suggested and am now getting the following Exception when I attempt to run the client:
tme4.PrimeGenServer PrimeGenServer Port already in use: 1099; nested exception is java.net.BindException: Address is already in use: JVM_Bind
I am assuming I am getting this because the RMI Registry is using port 1099. I checked before I ran the 'start rmiregistry' command and nothing was running on port 1099.
I am running on Windows [Vista 32-bit].
I guess the part of this that perplexes me is what is Netbeans doing that allows the Applet to run from the IDE with no issues?
Unfortunately I have no other option but to run this as an Applet as that is the requirement...
Try that and see it dislike that. You may get another error.
Joined: Aug 08, 2006
I made the change to the Applet code and now I'm getting the same error but it is referencing the 127.0.0.1:1099 IP, even though I hard coded my regular IP in the java.
Joined: Aug 08, 2006
So..... I finally got this working with the original code I had.
The problem the entire time was the java.policy file in the jre/lib/security folder. I had done a search for a 'java.policy' file on my PC when I was trying to diagnose the issue, but apparently the default search in Vista doesn't look at non-indexed files, even if you say to look at all files.
I replaced the default with a 'grant all permissions' file and voila everything is working! Now, I just need to figure out how to do this in a more appropriate fashion as I really don't want to be giving all permissions to all java files...
Thanks K. Tsang for all the pointers and assistance - I learned quite a bit during the whole process!
Nice to hear that you got it working. Well you are running applet and applets are bound by that sandbox security issue. I guess you can play around with the policy file and set it to the most secured way (ie allowing least privilege).