如果我使用来自不同位置的客户端调用此服务器方法,则会出现以下错误。如果我从服务器所在的同一位置运行客户端,我将获得输出。
请给出一些解决方案,用来自不同地点的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
代码======
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(){}
}}
如果我使用来自不同位置的客户端调用此服务器方法,则会出现以下错误。如果我从服务器所在的同一位置运行客户端,我将获得输出。 请给出一些解决方案,用来自不同地点的rmi客户端调用我的rmi服务器。
答案 0 :(得分:0)
您需要将异常中指定的存根类部署到客户端。
答案 1 :(得分:-1)
在RMI客户端
XslFoTransformerInterface stub =(XslFoTransformerInterface)命名 .lookup( “RMI://本地主机:1099 / XSL-Fo_Transform”);
您需要将localhost更改为服务器IP
答案 2 :(得分:-1)
我不确定您是否在不同位置(没有服务器计算机)运行您的程序,其中包含您为localhost硬编码的ip代码的更新版本。
如果这不是问题,您必须确保将客户端代码中的XslFoTransformerInterface类放在与服务器代码相同的包中。
此外,如果您将某些Serializable POJO类作为参数或远程方法的返回类型,则会应用相同的规则。始终将它们放在相同的包中,因为它们是服务器代码。
祝你好运。