RMI Concept: Performing code on other machines over the network, traditional methods are not only difficult to learn, but also error. The best way to solve this problem is: Some objects are just in another machine, we can send a message and get the return result, just like your own machine. Java Remote Method Call (RMI) Features Enables programs running on the client to invoke objects on the remote server. Remote Method Call Features Enables Java programmers to distribute operations in a network environment.
It has been in contact with Java, usually just writes JSP servlet bean. Suddenly heartbroquity, decided to go to the J2EE in other technologies, first javamail, this simple (嘿, actually just writing a few examples) I didn't encounter something that made me a headache. Next ejb, I feel very bored very bored by the practice of achieving EJB, a class is very bored (but it seems that EJB3.0 is not needed, I have already contacted, I have the opportunity to see Look at the information. Then it is RMI. It is like painted the gourd according to the book, the code is as follows:
Package RMI; Import Java.rmi. *; Public Interface PerfectTimei Extends Remote {Long getPerfectTime () throws RemoteException;}
package rmi; import java.net *;. import java.rmi *;. import java.rmi.registry *;. import java.rmi.server *;. public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI {public PerfectTime () throws RemoteException { super ();} public long getPerfectTime () throws RemoteException {return System.currentTimeMillis ();} public static void main (String [] args) throws Exception {System.setSecurityManager (new RMISecurityManager ()); java.rmi.registry. LocateRegistry.createRegistry (2005); try {PerfectTime Pt = new perfecttime (); naming.rebind ("// localhost: 2005 / perfecttime", pt); system.out.println ("Ready to do time");} catch (Exception E) {E.PrintStackTrace ();}}}
Package RMI; Import Java.rmi. *; Import Java.rmi. *;
public class DisplayPerfectTime {public DisplayPerfectTime () {super ();} public static void main (String [] args) {System.setSecurityManager (new RMISecurityManager ()); try {PerfectTimeI t = (PerfectTimeI) Naming.lookup ( "// LocalHost: 2005 / PerfectTime "); for (int i = 0; i <10; i ) {system.out.println (" PerfectTime: T. GetPerfectTime ());}}}}} catCH (Exception E) {e. PRINTSTACKTRACE ();}}} then run in JBX Click Run ??? Result $ # $% # !!% $ # java.rmi.stubnotfoundexception: stub class not found: rmi.perfecttime_stub; halo, encounter first Question. I forgot to generate stub and skel. I haven't found commands or options that can generate these two files in JBX ... Depressed !! It seems that it will only return to the original society with command line mode. Run CMD.EXE D: / RMI / CLASSES> RMIC PerfectTime? Enter Error: Class Test.perfecttime Not Found.1 Error ........... Mygod. I have seen this class of this class of you RMIC I didn't see it. What happened to I have a problem than I have a Class when I have just contacted Java. How did I solve it? I have solved it, @ _ @, I have to add ClassPath. Hurry to : / RMI / Classes / Add ClassPath (thank you for your reminder to my reminder in the middle). Run cmd.exed: / RMI / CLASSES> RMIC PerfectTime? Enter D: / RMI / Classes> Back to RMI Directory Next look. Haha really generated two files for PerfectTime_skel.class and PerfectTime_stub.class. .Y
Ok, return to JBX to continue run my program. This time I throw java.security.accessControlException: Access Denied (java.net.socketpermission 127.0.0.1:2005 Connect, Resolve) does not seem to perform this server program.? ?? !!! My own machine I execute the program to be authorized! TMD. Hearts have a countless side. There is no way to check out what is wrong.
As a result, some information is to be configured to configure a security policy. But they can't say it. It is also said to add parameters when calling. I have tried it one by one. I can't do it. The head is a bit numb. Since I learned Java No, I was tossing so long. I looked back and looked at the source code of the example. I found some code: System.setSecurityManager (New RMISecurityManager ()); the server and client use this sentence. I feel this Talking, don't, I think about comment. Continue Run's procedure ........ C: / bea / jdk141_05 / bin / javaw -classpath "D: / RMI / Classes; C: / bea / weblogic81 ..... Ready to do time
Yeah !!!! It was successfully run. Then I commented on which the client is also commented. Similarly, it is successful. Then I deleted the few security policy files you just added, in Run. Result is still successful! !