简体   繁体   English

Java远程方法调用

[英]Java Remote method invocation

If i call this server method with the client from different location iam getting following error.If i run the client from same location where the server is there I am getting output. 如果我从不同位置的客户端调用此服务器方法,则会出现以下错误。如果我从服务器所在的相同位置运行客户端,则会得到输出。

Please give some soltuions to call my rmi server with rmi client from different location. 请提供一些解决方案,以从不同位置使用rmi客户端调用我的rmi服务器。

Error
Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: com.transform.xsl.XslFoTransformer_Stub (no security manager: RMI class loader disabled)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Naming.java:101)
    at com.transform.xsl.RmiClient.main(RmiClient.java:15)
Caused by: java.lang.ClassNotFoundException: com.transform.xsl.XslFoTransformer_Stub (no security manager: RMI class loader disabled)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:394)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
    at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
    at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
    at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:222)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1610)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    ... 3 more

Code ====== 代码======

import java.io.File;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface XslFoTransformerInterface extends Remote {
    public byte[] doTransform(File stylesheet, File datafile) throws Throwable,RemoteException;

}

public class XslFoTransformer extends UnicastRemoteObject  implements XslFoTransformerInterface {

    protected XslFoTransformer() throws RemoteException {
        super();

    }

    public byte[] doTransform(File stylesheet, File datafile) throws Exception {

        String fileName = datafile.getName();
        int pos = fileName.lastIndexOf(".");
        if (pos > 0) {
            fileName = fileName.substring(0, pos);

        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        File file = new File(fileName + ".xsl-fo");
        try {
            // File stylesheet = new
            // File("C:\\Users\\Q811213\\Documents\\XSLT\\files\\ECGSearchResultsPDF_changed1.xsl");
            // File datafile = new
            // File("C:\\Users\\Q811213\\Documents\\XSLT\\files\\NEWDATA.xml");
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(datafile);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = tFactory.newTransformer(stylesource);
            DOMSource source = new DOMSource(document);
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            StreamResult result=new StreamResult(bos);
            transformer.transform(source, result);
             byte [] array=bos.toByteArray();


        return array;
        } catch (Exception e) {
            throw e;
        }}}


--Rmi server class


public class RmiServer {
    public static void main(String args[]) throws Exception{  
        try{  


        XslFoTransformerInterface stub=new XslFoTransformer();  
        Naming.rebind("rmi://localhost:1099/Xsl-Fo_Transform",stub);  
        }catch(Exception e){
            throw e;

        }  
        }  

}


--Rmi client created in different location



public class RmiClient {
    public static void main(String args[]) throws Throwable {
        try {
            XslFoTransformerInterface stub = (XslFoTransformerInterface) Naming
                    .lookup("rmi://localhost:1099/Xsl-Fo_Transform");
            System.out.println("stub is ready" + stub);
            File f1 = new File(
                    "C:\\Users\\s811213\\osbwstest\\XSL-FO_Transformer\\files\\ECGSearchResultsPDF_changed1.xsl");
            File f2 = new File(`enter code here`
                    "C:\\Users\\s811213\\osbwstest\\XSL-FO_Transformer\\files\\NEWDATA.xml");
            try {
                byte[] f = stub.doTransform(f1, f2);
                }cartch(){}

}}

If i call this server method with the client from different location iam getting following error.If i run the client from same location where the server is there i am getting output. 如果我从不同位置的客户端调用此服务器方法,则会出现以下错误。如果我从服务器所在的相同位置运行客户端,则会得到输出。 Please give some soltuions to call my rmi server with rmi client from different location. 请提供一些解决方案,以从不同位置使用rmi客户端调用我的rmi服务器。

您需要将异常中命名的存根类部署到客户端。

In RMI client 在RMI客户端中

XslFoTransformerInterface stub = (XslFoTransformerInterface) Naming .lookup("rmi://localhost:1099/Xsl-Fo_Transform"); XslFoTransformerInterface存根=(XslFoTransformerInterface)命名.lookup(“ rmi:// localhost:1099 / Xsl-Fo_Transform”);

you need to change localhost to server ip 您需要将localhost更改为服务器ip

I'm not sure if you running your program in different locations (no the server machine) with the updated version of your code for ip which you hard coded for localhost. 我不确定您是否在其他位置(没有服务器计算机)运行程序,并且使用localhost硬编码的ip代码的更新版本。

If this is not the problem, you must be sure that you placed the XslFoTransformerInterface class in your client codes in the same package as it is in server codes . 如果这不是问题,则必须确保将XslFoTransformerInterface类放在客户端代码中,与服务器代码中的包相同

Also in the future if you have some Serializable POJO classes as the arguments or the return types of you remote methods, same rule is applied for them. 同样在将来,如果您有一些Serializable POJO类作为参数或远程方法的返回类型,则对它们应用相同的规则。 Always put them in the same package as they reside is the server codes. 始终将它们与服务器代码放在相同的包装中。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM