为什么我找到一个没有找到exportObject错误的合适方法?

时间:2012-02-23 20:49:01

标签: java parallel-processing rmi

我收到以下错误:

no suitable method found for exportObject(exame2010.pkg2011.perfect,int)

RMIServer

的这一行
perfectInterface stub = (perfectInterface) UnicastRemoteObject.exportObject(obj, 0);

相关代码如下:


public class RMIServer
{
    public static void main(String[] args)
    {
        try
        {
            perfect obj = new perfect();
            perfectInterface stub = (perfectInterface) UnicastRemoteObject
                    .exportObject(obj, 0);
            Registry registry = LocateRegistry.createRegistry(10000);
            registry.bind("RemoteClass", stub);
            System.out.println("Server start");
        }
        catch (Exception ex)
        {
        }
    }
}

public class RMIClient
{
    public static void main(String[] args)
    {
        try
        {
            Registry registry = LocateRegistry.getRegistry("localhost", 10000);
            perfectInterface stub = (perfectInterface) registry
                    .lookup("RemoteClass");
            System.out.println("MENSAGEM " + stub.getperfect());
        }
        catch (Exception ex)
        {
        }
    }
}

public interface perfectInterface extends Remote
{
    public List<Integer> getperfect() throws RemoteException;
}

public class perfect extends Thread implements Serializable
{
    public List<Integer> getperfect() throws RemoteException
    {
        return Exame20102011.calcula();
    }
}

public class Exame20102011 extends Thread implements Serializable
{
    List<Integer> perfect = new ArrayList<Integer>();
    int ini = 0;
    int fim;
    int sum;

    public Exame20102011(int ini, int fim)
    {
        this.ini = ini;
        this.fim = fim;
    }

    public void run()
    {
        for (int number = ini; number < fim; number++)
        {
            sum = 0;
            for (int J = 1; J < number; J++)
            {
                if (number % J == 0)
                {
                    sum += J;
                }
            }
            if (sum == number)
            {
                perfect.add(number);
            }
        }
    }

    public static List<Integer> calcula()
    {
        Scanner LIMIT = new Scanner(System.in);
        String Limit = LIMIT.nextLine();

        int dim = Integer.parseInt(Limit) / 8;

        Exame20102011[] calculaPerfeitos = new Exame20102011[8];

        for (int i = 0; i < calculaPerfeitos.length; i++)
        {
            calculaPerfeitos[i] = new Exame20102011(i * dim, (i + 1) * dim);
        }

        for (int i = 0; i < calculaPerfeitos.length; i++)
        {
            calculaPerfeitos[i].start();
        }

        for (int i = 0; i < calculaPerfeitos.length; i++)
        {
            try
            {
                calculaPerfeitos[i].join();
            }
            catch (InterruptedException ex)
            {
                Logger.getLogger(Exame20102011.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }

        List<Integer> perfect = new ArrayList<Integer>();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < calculaPerfeitos[i].perfect.size(); j++)
            {
                perfect.add(calculaPerfeitos[i].perfect.get(j));
            }
        }
        return perfect;
    }

    public static void main(String[] args)
    {
        List<Integer> perfect = new ArrayList<Integer>();
        perfect = calcula();
        for (int i = 0; i < perfect.size(); i++)
        {
            System.out.println("Numero perfeitos calculados = "
                    + perfect.get(i));
        }
    }
}

有什么问题?

1 个答案:

答案 0 :(得分:2)

错误消息表示类UnicastRemoteObject没有静态方法exportObject(perfect,int)。这是因为课程perfect没有实现perfectInterface,所以它没有实现Remote