无法从RMI客户端查找远程对象

时间:2015-02-09 08:12:06

标签: java file-io rmi

我正在尝试编写一个基本的远程对象,它将添加两个数字。

我的远程接口:

package com.tcs.rmiServer;
import java.rmi.*;
public interface AdditionRemoteInterface extends Remote {

    public int add(int x, int y)  throws RemoteException;
}

我的远程对象:

package com.tcs.rmiServer;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class AdditionRemoteObject extends UnicastRemoteObject implements AdditionRemoteInterface {
    AdditionRemoteObject()  throws RemoteException
    {
    super();
    }
    public int add(int x, int y)
    {
        return x+y;
    }

}

我的服务器代码:

package com.tcs.rmiServer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class AdditionServer {

    public static void main(String[] args) throws RemoteException {
        // System.setSecurityManager(new RMISecurityManager());
        // AdditionRemoteObject addObject = new AdditionRemoteObject();
        // try {
        // Naming.bind("rmi://localhost/add", addObject);
        // } catch (MalformedURLException e) {
        // System.out.println("Server failed");
        // e.printStackTrace();
        // } catch (AlreadyBoundException e) {
        // System.out.println("Server failed");
        // e.printStackTrace();
        // }
        //System.setProperty("java.security.policy", getLocationOfPolicyFile());

        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }

        AdditionRemoteInterface addObject = new AdditionRemoteObject();
        AdditionRemoteInterface stub = (AdditionRemoteInterface) UnicastRemoteObject
                .exportObject(addObject, 0);

        Registry registry = LocateRegistry.getRegistry();
        try {
            registry.bind("Add", stub);
        } catch (AlreadyBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

我的客户:

package com.tcs.rmiClient;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class AdditionClient  {

    public static void main(String args[]) throws MalformedURLException, RemoteException, NotBoundException
    {
//  AdditionRemoteInterface add = (AdditionRemoteInterface)Naming.lookup("rmi://localhost/add");
//  int result = add.add(3, 2);
//  System.out.println("Result is: "+ result);
        System.setProperty("java.security.policy", getLocationOfPolicyFile());
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }

        try {
            Registry registry = LocateRegistry.getRegistry();
            AdditionRemoteInterface object = (AdditionRemoteInterface) registry.lookup("Add");
            System.out.println(object.add(2, 3));
        } catch (Exception e) {
            System.err.println("Exception:");
            e.printStackTrace();
        }
    }

    public static String getLocationOfPolicyFile() {

        String POLICY_FILE_NAME = "security.policy";
        File tempFile;
        try {
            tempFile = File.createTempFile("rmi-base", ".policy");

            InputStream is = new FileInputStream(POLICY_FILE_NAME);
            BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
            int read = 0;
            while ((read = is.read()) != -1) {
                writer.write(read);
            }
            writer.close();
            tempFile.deleteOnExit();
            return tempFile.getAbsolutePath();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return "";
    }



}

当我在security.policy中添加System.propery时,我收到此错误:

java.security.policy: error parsing file:/C:/Users/PalSu001/AppData/Local/Temp/rmi-base6979340421200722112.policy:
    expected [;], read [end of file]

当我在security.policy中评论添加System.propery的代码时,我收到此错误:

java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
    at java.security.AccessController.checkPermission(AccessController.java:559)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
    at java.net.Socket.connect(Socket.java:574)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at com.tcs.rmiClient.AdditionClient.main(AdditionClient.java:28)

请有人帮我这个。感谢。

0 个答案:

没有答案