Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 641923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:06:55+00:00 2026-05-13T21:06:55+00:00

I am just starting Java RMI and have some problems with when to use

  • 0

I am just starting Java RMI and have some problems with when to use java.io.Serializable, so can anyone give me a RMI example that java.io.Serializable has to be implemented.

Thanks!!!


UPDATE:
i had made a simple example, however, i think there are still problems as the output is not correct.
Person Interface

package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public interface PersonInterface extends Remote  
{
    public void setName(String name) throws RemoteException;
    public String getPerson() throws RemoteException;
    public void setAddress(Address address) throws RemoteException;
}

Person Implementation

package server;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.Remote;

class Person extends UnicastRemoteObject implements PersonInterface
{
    private String name;
    private int age;
    private Address address;


    Person() throws RemoteException {super();}
    Person(String name,int age, Address address) throws RemoteException {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public void setName(String name) throws RemoteException {
        this.name = name;
    }
    public void setAddress(Address address) throws RemoteException{
        this.address = address;
    }

    public String getPerson() throws RemoteException {
        return "Person : " + name + " age : " + age + " address : " + address;
    }
}

Address Class

package server;
import java.io.Serializable;

public class Address implements Serializable
{
    private static final long serialVersionUID = 227L;
    private String addre1;
    private String addre2;

    public Address() {}
    public Address(String addre1,String addre2){
        this.addre1 = addre1;
        this.addre2 = addre2;   
    }
}

Server

package server;
import java.rmi.Naming;

class Server 
{
    public static void main(String[] args) 
    {
        try{
        //create an instance of the RemoteDatabaseServer
            Person person = new Person();
            //rmi://[host][:port]/object
            String namePerson = "rmi://localhost:9999/person";

            //bind this instance to localhost port999 with name database
            Naming.bind(namePerson, person);
            System.out.println("Server is running...");
        }catch(Exception ex){
            System.out.println("Server Exception...");
            ex.printStackTrace();
        }
    }
}

Client

package client;
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import server.PersonInterface;
import server.Address;


class Client
{
    public static void main(String[] args) 
    {
        try{
            System.setSecurityManager(new RMISecurityManager());
            String namePerson = "rmi://localhost:9999/person";
            PersonInterface person = 
                (PersonInterface)Naming.lookup(namePerson);

            person.setName("myName");
            System.out.println(person.getPerson());
            person.setName("myNewName");
            Address address = new Address("123","123");
            person.setAddress(address);
            System.out.println(person.getPerson());
        }catch(Exception ex){
            System.out.println("Client failure...");
            ex.printStackTrace();
        }
    }
}

The output i got is

D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client
Person : myName age : 0 address : server.Address@1d6776d
Person : myNewName age : 0 address : server.Address@10a2d64

The address is not printed correctly
PS:
As you can see from Client class import

import server.PersonInterface;
import server.Address;

I had copy PersonInterface.class and Address.class to client side to make Client compiled.


Final:
So stupid!!!
Add following code to Address.java

public String toString(){
    return addre1+ " " + addre2;
}

OK, problems are solved!! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:06:55+00:00Added an answer on May 13, 2026 at 9:06 pm
    interface MyInterface extends Remote {
      MyClass f(MyClass x) throws RemoteException;
    }
    
    class MyClass implements Serializable {
       private int value;
       public MyClass(int value) {
           this.value = value;
       }
    
       public int getValue() {
           return value;
       }
    }
    

    you need Serializable interface to tell that your class can be sent via network

    server code

    class Service extends UnicastRemoteObject  implements MyInterface {
       public Service() {
       }
       public MyClass f(MyClass v) throws RemoteException {
           return new MyClass(v.getValue() + 1)
       }
    
       public static void main(Strint arg[]) {
          Registry r = LocateRegistry.createRegistry(1099);
          r.rebind("service", new Service());
       }
    }
    

    client code

    class Client {
           public static void main(Strint arg[]) {
              Registry r = LocateRegistry.getRegistry("localhost", 1099);
              MyInterface service = (MyInterface)r.lookup("service");
    
              MyClass result = service.f(new MyClass(123));
              System.out.println(result.getValue());  //print 124 here
           }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You want Longs returned? RowOffset = B.Cells(1,1).Row - A.Cells(1,1).Row ColOffset… May 14, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer Can you just hide the second <div id="sidebar"> it doesn't… May 14, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer IE 6 doesn't support position: fixed (Source) and there is… May 14, 2026 at 9:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.