i have written the code in client,server,interfaces.i have created stubs and skaleton.
i have run rmiregistry very well.i have run server.i have an error in client side,when i run client.it is moving to the catch not looking up to the correct address of server.
the code of client is:

import java.rmi.*;
import java.rmi.server.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;



public class RMIClient 
{
    public static void main(String argv[])
    {  System.setSecurityManager(new RMISecurityManager());
       RMIExample RemRMIExample = null;

      try
        {   
            RemRMIExample = (RMIExample)Naming.lookup("rmi://localhost/hi");
            System.out.println(RemRMIExample.hello());
        }
        catch (Exception e)
        {
        System.out.println("Client: Exception thrown looking up ");
        System.exit(1);
        }
    
     try
    {
    RemRMIExample.hello();
    }
    catch (Exception e)
    {System.out.println("Client: Exception thrown calling PostMsg().");
     System.exit(1);

    }
}
}

my questio in 2 parts:
1. in case where client and server on same machine what must i type,is this correct:
RemRMIExample = (RMIExample)Naming.lookup("rmi://localhost/hi");
bcs it is not looking for correct server.and client is not running correctly.
2.in remote case what must i type.
thanxs,i appreciate any help

The format acceptable by the Naming.lookup method is mentioned in the javadocs of the Naming class. For more information on binding a service to a given name and looking it up when writing the client, refer: rmi server tutorial, rmi client tutorial

Follow the instructions mentioned and re-post again if facing problems with the *complete* stack trace.

EDIT: Also, the concept of Skeletons & stubs has been deprecated. This getting started guide creates a simple client and server without using the deprecated rmic (rmi compiler) for creating stubs.

Edited 6 Years Ago by ~s.o.s~: n/a

hi,i apreciate ur response.
i have read the tutorials in the links above,now i am receiving an error access denied,java.net.socket permission exception localhost:1099
also in the same error message java,security.Access control.checkpermission
......
i receive the message when i run client side
best regards

Like already mentioned in my previous post, paste the *entire* stack trace instead of just bits and pieces. Also, if you have made any code changes, post them and make sure that the lines numbers in the stack trace match with those of the posted code.

code of interface

import java.rmi.*;

public interface RMIExample extends Remote
{
        public String hello() throws RemoteException;
}

code of server:

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.io.*;

public class RMIExampleImpl extends UnicastRemoteObject implements RMIExample{
protected static String m_strName;
public RMIExampleImpl() throws RemoteException
    {
        super(); // call base class constructor
    }
public String hello()
{return("hello world");
}
 public static void main(String argv[])
    { if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }

        try
        {   
            RMIExampleImpl Example = new RMIExampleImpl();
            Naming.rebind("hi", Example);
            System.out.println("server ready");
        }
        catch (Exception e)
        {
            System.out.println("Server: Failed to register RMIExampleImpl: " + e);
        }
    }
}

code:

import java.rmi.*;
import java.rmi.server.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;

code of client:

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.math.BigDecimal;

public class client {
    public static void main(String args[]) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "hi";
            Registry registry = LocateRegistry.getRegistry(args[0]);
            RMIExample comp = (RMIExample) registry.lookup(name);
            System.out.println(comp.hello());

        } catch (Exception e) {
            System.err.println("ComputePi exception:");
            e.printStackTrace();
        }
    }    
}

i have changed the code of the client from that i've written in my first post to the when i've written it now after reading the above links.
i am compiling interface,client,server

javac RMIExample.java,RMIExampleImpl.java,client.java

then creating stub,skeleton using command

rmic RMIExampleImpl

then running registry first as rmiregistry

running server java RMIExampleImpl

without problems till here
when i run the client as java client i receive the error message.

thanx,best regards

Edited 3 Years Ago by mike_2000_17: Fixed formatting

It seems that you still haven't read the links I posted since the links clearly make no mention of "rmic". Also, when I say complete stack trace, you need to post the "entire stack trace" which you see on your console or IDE window instead of just the "exception message".

Anyways, try running your RMI service on a higher end port like 12345 and see if it still gives the same problem.

thanx for response
it goes well locally and with the old code that i've written,and what causes the error was this statment,
System.setSecurityManager(new SecurityManager());
when i eliminate it every thing goes well.
but now rises another question,what do you think if i want to run it remotely,bcs this is my goal,i tried it first locally.
what must i do?

You specify a SecurityManager but don't specify a custom policy file which is what is causing the problem here since the "default" java.policy file which comes with the JRE hasn't got the entry which allows you to "connect" to a socket.

The solution here would be to specify your own policy file by creating a new one and specifying it via the -Djava.security.policy="path/to/policy/file". Read the Java security guide for more details. As a start, you can create a policy file which gives the executing Java process all permissions:

grant { 
    permission java.security.AllPermission;
};

Save this piece of code in a file and name it my.policy. Specify the location to this file when using -D switch in the JVM arguments and you should be good to go.

In case your RMI server would be executing untrusted client code, make sure you create a well-thought out security file for the server to ensure that clients from executing arbitrary unsafe code.

Edited 6 Years Ago by ~s.o.s~: n/a

thanx for response,
u mean that i create in client's and server's classpath(c:\java) a file for example named java.policy,and
put in it

grant {     permission java.security.AllPermission;};

the i add to the client's and server's code
System.setSecurityManager(newSecurityManager()); and i run client and server as:
java -Djava.security.policy=c:\java\java.policy RMIServer.java
java -Djava.security.policy=c:\java\java.policy RMIClient.java (name of host)
best regards

execuse me when running i put:
java -Djava.security.policy=c:\java\java.policy RMIServer
java -Djava.security.policy=c:\java\java.policy RMIClient (name of host)
best regards

Yeah, why not try it out? Also, just to be sure, enclose the values in quotes and use forward instead of back slashes:

java -Djava.security.policy="c:/my.policy" YourClass

i try it in same machine,i create java.policy,then....
it works fine,when i move the java.policy,stub class,client,interface to another machine,i run the registry,server.when i run the client i receive the old error.
exception is thrown.
i run it on the client machine as:java -Djava.security.policy RMIClient
best regards

Read my previous post again; you also need to specify the path of your policy file, not just the system property. It should be:

java -Djava.security.manager -Djava.security.policy="c:/default.policy" RMIClient

i've specified the policy's path.maybe i forgot to put it in my previous post.
bcs when client and server are in same machine,the policy file was in:c:\java\rmi as an example,i've written:java -Djava.security.policy="java.policy" RMIClient and it works fine.But when client and server resides on different machine,it is giving me an exception.i don't know what is the problem.must i change the port from default port 1099,or problem of firewall,or security managemenet.
thanx

i've specified the policy's path.maybe i forgot to put it in my previous post.
bcs when client and server are in same machine,the policy file was in:c:\java\rmi as an example,i've written:java -Djava.security.policy="c:\java\rmi\java.policy" RMIClient and it works fine.But when client and server resides on different machine,it is giving me an exception.i don't know what is the problem.must i change the port from default port 1099,or problem of firewall,or security managemenet.

import java.rmi.*;
import java.rmi.server.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;



public class RMIClient 
{
    public static void main(String argv[])
    {  
       RMIExample RemRMIExample = null;

      try
        {   String name = "//localhost/hi";
            RMISecurityManager security =new RMISecurityManager();
            System.setSecurityManager(security);
            RemRMIExample = (RMIExample)Naming.lookup(name);
            System.out.println(RemRMIExample.hello());
        }
        catch (Exception e)
        {
        System.out.println("Client: Exception thrown looking up ");
        System.exit(1);
        }
    
     try
    {
    RemRMIExample.hello();
    }
    catch (Exception e)
    {System.out.println("Client: Exception thrown calling PostMsg().");
     System.exit(1);

    }
}
}

the error is "Client: exception thrown"

thanx

Instead of having a sysout in your catch blocks, put in e.printStackTrace() and post the entire stack trace which comes up on your console (or ide output window). And please post the entire thing, not just the error message.

the arror i receives when put in e.printStackTrace() is:

java.rmi.ConnectException: Connection refused to host: 192.168.1.3; nested excep
tion is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at RMIExampleImpl_Stub.hello(Unknown Source)
at RMIClient.main(RMIClient.java:21)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)

This is a different error; seems more of a networking issue than a Java one. Are you able to ping that host or telnet to that host for the given port?

ping 192.168.1.3
telnet 192.168.1.3 your-port

There are many reasons why you might get a timeout like bad network configuration, firewalls, overloaded server, bad host or port etc.

Edited 6 Years Ago by ~s.o.s~: n/a

thanx my freind
i have disabled firewall on client machine,every thing goes well.i received the message "hello world successfully".
by the way i am registered in dyndns bcs i have dynamic ip address.and it goes well.
i think is it possible to create method void(),and call this method through remote object using rmi?
as if i am using jdbc to access database for example remotely.i can also use rmi to achieve this?
best regards

You can have any number/kinds of methods in your RMI server implementation as long as they follow the RMI specification (namely having an interface which extends Remote interface, methods should throw RemoteException, Objects of classes used for communication need to be serializable etc.)

u mean,I send or return, any object that implement serializable as parameter between rmi client and rmi server.or any object of a class that derived from objectinput,object output stream.and having an interface which extends Remote interface and methods that throw RemoteException.
thanx

Yes for Serializable (Externalizable) and Remote interface. I'm not sure what you mean by "derived from ObjectInput ObjectOutputStream" since object reading/writing is done behind the scenes...

BTW, why not try it out and write a sample server by reading the tutorials posted in one of my previous posts? You'd learn more by doing than just asking...

This question has already been answered. Start a new discussion instead.