java RMI怎么把客户端和服务器端分到两个project里,我用的是netbeans
我把interface的类分别放到了client的project和server的project里了,实现的类放在了server的project里,client和serve...
我把interface的类分别放到了client的project 和server的project里了, 实现的类放在了server的project里,client 和server 再各有一个class。
代码如下:
interface的
import java.rmi.*;
public interface CommandIF extends Remote{
public String getGreeting() throws RemoteException;
}
实现类的
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CommandImp extends UnicastRemoteObject implements CommandIF{
public CommandImp() throws RemoteException
{
super();
}
@Override
public String getGreeting() throws RemoteException
{
return "Hello There!";
}
}
客户端的class
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import javax.swing.JOptionPane;
public class RMIClient {
public static void main(String[] args) {
try{
System.setSecurityManager(new RMISecurityManager());
CommandIF hello = (CommandIF)Naming.lookup("rmi://localhost/hello");
JOptionPane.showMessageDialog(null, hello.getGreeting());
}catch(Exception e){
System.out.println(e);
}
}
}
服务器端的class
import java.rmi.Naming;
import javax.swing.JOptionPane;
public class RMIServer {
public static void main(String[] args) throws Exception{
CommandImp temp = new CommandImp();
String rmiObject = "rmi://localhost/hello";
Naming.rebind(rmiObject, temp);
JOptionPane.showMessageDialog(null, "binding completed");
}
}
我从网上找到的 可以直接在netbeans里启动rmi,就是修改build.xml文件,添加这个。如下:
<target name="startRMI" depends="init">
<exec executable="rmiregistry" dir="${build.classes.dir}">
</exec>
</target>
这样都弄完后,我通过build.xml那运行了rmi,然后再运行server的那个class,接着再运行client的class的话就会报错
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmiserver.CommandIF
rmiserver就是我的server project的package,CommandIF就是那个interface。
请问这是怎么回事? 网上有人说是在classpath里加入这个class的路径,但怎么从netbeans里修改classpath啊?
请帮帮忙 展开
代码如下:
interface的
import java.rmi.*;
public interface CommandIF extends Remote{
public String getGreeting() throws RemoteException;
}
实现类的
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CommandImp extends UnicastRemoteObject implements CommandIF{
public CommandImp() throws RemoteException
{
super();
}
@Override
public String getGreeting() throws RemoteException
{
return "Hello There!";
}
}
客户端的class
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import javax.swing.JOptionPane;
public class RMIClient {
public static void main(String[] args) {
try{
System.setSecurityManager(new RMISecurityManager());
CommandIF hello = (CommandIF)Naming.lookup("rmi://localhost/hello");
JOptionPane.showMessageDialog(null, hello.getGreeting());
}catch(Exception e){
System.out.println(e);
}
}
}
服务器端的class
import java.rmi.Naming;
import javax.swing.JOptionPane;
public class RMIServer {
public static void main(String[] args) throws Exception{
CommandImp temp = new CommandImp();
String rmiObject = "rmi://localhost/hello";
Naming.rebind(rmiObject, temp);
JOptionPane.showMessageDialog(null, "binding completed");
}
}
我从网上找到的 可以直接在netbeans里启动rmi,就是修改build.xml文件,添加这个。如下:
<target name="startRMI" depends="init">
<exec executable="rmiregistry" dir="${build.classes.dir}">
</exec>
</target>
这样都弄完后,我通过build.xml那运行了rmi,然后再运行server的那个class,接着再运行client的class的话就会报错
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmiserver.CommandIF
rmiserver就是我的server project的package,CommandIF就是那个interface。
请问这是怎么回事? 网上有人说是在classpath里加入这个class的路径,但怎么从netbeans里修改classpath啊?
请帮帮忙 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询