I am learning RMI concepts and had built a simple program taking reference from head first java. All Went fine the first time i ran the code through command prompt.
the next time I ran code the command:
rmiregistry
took too long to load and nothing happened.
I even tried the solution in this thread but nothing happend.
need help to run RMI Registry
also when i run my server and client file i get this error:
Exception: java.rmi.ConnectException: Connection refused to host: 192.168.1.105; nested exception is:
java.net.ConnectException: Connection refused: connect
My Source Code:
myremote.java
import java.rmi.*;
public interface myremote extends Remote
{
public String sayhello() throws RemoteException;
}
Server.java
import java.rmi.*;
import java.rmi.server.*;
public class Server extends UnicastRemoteObject implements myremote
{
public Server() throws RemoteException{}
public String sayhello()
{
return("Server says hi");
}
public static void main(String args[])
{
try
{
myremote S = new Server();
Naming.rebind("remotehello",S);
}
catch(Exception E)
{
System.out.println("Exception: "+E);
}
}
}
client.java
import java.rmi.*;
public class client
{
public static void main(String[] args)
{
client c = new client();
c.go();
}
public void go()
{
try
{
myremote S=(myremote) Naming.lookup("rmi://127.0.0.1/remotehello");
System.out.println("Output:"+S.sayhello());
}
catch(Exception E)
{
System.out.println("Exception: "+ E);
}
}
}
Can't run RMI registry on my system
You've provided no evidence of that.
took too long to load
I don't know what this means. Evidence?
nothing happened.
Nothing is supposed to happen. The RMI registry doesn't print anything. It just sits there.
Run it and try again. You'll be surprised.
Related
I am using IntelliJ as my IDE and the code below runs alright if they are in the same src folder. However, what I want is to call the sayHello() method in another project. Is that possible? I thought this is possible since this is what RMI enables, but am I wrong?
I tried to create another project that contains a Main java class and has the same code as the Client Test Drive below, hoping to call the sayHello() method by utilizing Naming.lookup() but it doesn't work! If I try to run it, I was given a java.rmi.UnmarshalException: error unmarshalling return; exception. What am I missing?
How can I call the sayHello() method "remotely"?
Remote Interface:
package Remote;
import java.rmi.*;
public interface HelloRemote extends Remote {
String sayHello() throws RemoteException;
}
Remote Implementation
package Remote;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class HelloRemoteImpl extends UnicastRemoteObject implements HelloRemote{
public HelloRemoteImpl() throws RemoteException {};
#Override
public String sayHello() throws RemoteException {
return "Server says, \"Hello!\"";
}
public static void main(String[] args) {
try {
// 2.3 register the service
HelloRemote service = new HelloRemoteImpl();
final int PORT = 1888;
Registry registry = LocateRegistry.createRegistry(PORT);
registry.rebind("hello", service);
System.out.println("Service running on PORT: " + PORT);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Client Test Drive
package Remote;
import java.rmi.Naming;
public class SayHelloTest {
public static void main(String[] args) {
try {
HelloRemote service = (HelloRemote) Naming.lookup("rmi://127.0.0.1:1888/hello");
String helloStr = service.sayHello();
System.out.println(helloStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am creating RMI program for my class assignment in Netbeans. It is a simple RMI program and The server side is working properly. But as I run my client side file. It ends up giving me error
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
plus it is saying some error at line 26 at client code.
For clear understanding I am giving full code of all three files.
Interface.java :
package RMI;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface DemoInterface extends Remote {
public String SayDemo() throws RemoteException;
}
Server.java
package RMI;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Interface{
public Server()
{
super();
}
private String message;
public Server(String msg) throws RemoteException
{
message = msg;
}
public static void main(String[] args) {
try {
DemoInterface h = new Server("Hello");
DemoInterface stub = (DemoInterface) UnicastRemoteObject.exportObject(h,0);
LocateRegistry.createRegistry(4096);
Registry registry = LocateRegistry.getRegistry("127.0.0.1",4096);
registry.rebind("Hello", stub);
System.out.println("Server is connected and ready to use");
}
catch(Exception e)
{
System.out.println("server not connected\n"+e);
}
}
#Override
public String SayDemo() throws RemoteException {
System.out.println("Server.saydemo override");
return message;
}
}
Client.java
package RMI;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
if(System.getSecurityManager() == null)
{
System.setSecurityManager(new SecurityManager());
}
try {
Registry reg = LocateRegistry.getRegistry("127.0.0.1", 4096);
System.out.println("in try after reg locate");
DemoInterface h = (DemoInterface) reg.lookup("Hello");//Error Showed on this line by netbeans
System.out.println(h.SayDemo());
}
catch(RemoteException | NotBoundException e)
{
System.out.println(""+e );
}
}
}
please guide me where I am wrong. Thank You in advance.
You set a SecurityManager in your client main method. Did you also provide a security policy file? The default policy is not very permissive, and denies, among other things, Socket operations.
You can specify a policy that allows all permissions to all code bases like so.
grant {
permission java.security.AllPermission;
};
add it to your command line for invoking java. Substitute mypolicy for your policy file and SomeApp for your main class. Note the two = characters in the second argument
java -Djava.security.manager -Djava.security.policy==mypolicy SomeApp
Note that this is not a safe policy to run for RMI in a production environment (RMI can load remote code bases).
Proper use of the SecurityManager class and policy configuration is a complex topic, for further reading I suggest Java SE 7 Security Documentation and in particular Default Policy Implementation and Policy File Syntax
I get a remote exception when I try to run my RMI example. I cannot understand why.
I run the program without any arguments to the program itself or to the JVM.
Please help me to get rid of the exception.
Thanks very much
This is the exception I get:
Server exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: hello.Hello
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: hello.Hello
These are the classes I have:
The client class:
package hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
The remote interace:
package hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
And finally the server:
package hello;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry("localhost");
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Either the Registry or the client or both cannot find the class named in the exception. There are several possible solutions:
include that class in the classpath when executing the Registry and the client. And all the classes it depends on, recursively until closure.
Start the Registry from inside the server JVM, with LocateRegistry.createRegistry(), which solves that classpath problem, and provide the necessary classes on the client's classpath only.
Use the codebase feature to ensure all the system components can access the required server-side classes.
As outlined in the RMI trail
You may need to supply the java.rmi.server.codebase parameter that list all the jars that the RMI server needs to export the objects...
See Running the examples and look at the "Starting the server" section. Also be sure to check out the section on running the client program for additional suggested parameters
i have created one simple java RMI program for understanding how it works.But when i trying to run my server side it raising the following exception.
EDIT: We are using proxy connection...
Remote exception: java.rmi.ConnectException: Connection refused to host: 10.7.150.18; nested exception is:
java.net.ConnectException: Connection refused: connect
This is my Server side code for your reference...
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
#Override
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
public static void main(String[] args)
{
try
{
//set the security manager
//System.setSecurityManager(new RMISecurityManager());
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("//10.7.150.18:9999" , Server);
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me)
{
System.out.println("Malformed URL: " + me.toString());
}
catch (RemoteException re)
{
System.out.println("Remote exception: " + re.toString());
}
}
}
Please guide me to get out of this issue...
The Naming class provides methods for storing and obtaining references to remote objects in a remote object registry. Each method of the Naming class takes as one of its arguments a name that is a java.lang.String in URL format (without the scheme component) of the form:
//host:port/name
add a name with in the URL format
//10.7.150.18:9999/Server
I am new to Java RMI and I am simply trying to run a "Hello World" program (code is shown at the end of the message)
Basically, I have a remote class, a remote interface, and a server class in one of my computers and a client class in another computer.
I am trying to get a "hello" message from the server using the client.
The problem is that I cannot compile the client and get it running if I don't have the remote interface and the stub in the same directory where the client is, and at the same time I cannot run the server if I don't have those in the same directory that the server is.
I compiled the server/remote class/interface using javac and then using the rmic compiler.
"rmic Hello".
I am wondering how I could get this to work without having to have all the files in both computers (which is why I want to make it distributed)
Thanks in advance!
Code:
Remote Interface:
import java.rmi.*;
//Remote Interface for the "Hello, world!" example.
public interface HelloInterface extends Remote {
public String say() throws RemoteException;
}
Remote class:
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject implements HelloInterface {
private String message;
public Hello (String msg) throws RemoteException {
message = msg;
}
public String say() throws RemoteException {
return message;
}
}
Client:
import java.rmi.*;
public class Client
{
public static void main (String[] argv)
{
try
{
HelloInterface hello= (HelloInterface) Naming.lookup(host); //the string representing the host was modified to be posted here
System.out.println (hello.say());
}
catch (Exception e)
{
System.out.println ("Hello Server exception: " + e);
}
}
}
Server:
public static void main (String[] argv) {
try {
Naming.rebind ("Hello", new Hello ("Hello, world!"));
System.out.println ("Hello Server is ready.");
} catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
My guess would be to simply create identical source at both / either end.
am wondering how I could get this to work without having to have all the files in both computers
You can't. You have to distribute the required class files to the client.
(which is why I want to make it distributed)
Non sequitur.