我想编写一个应用程序以使用java rmi在客户机和服务器之间共享桌面。这是接口:帮助解决java rmi分配中的问题
public interface IServer extends Remote{
public void share(BufferedImage image) throws RemoteException;
}
-----------------------------
//这是服务器端代码:
public class ServerFrame extends JFrame implements IServer{
public static void main(String args[]) {
try {
ServerFrame frame = new ServerFrame();
frame.setVisible(true);
LocateRegistry.createRegistry(1099);
Naming.bind("test", frame);
System.out.println("Server Started Successfully...");
} catch (Exception e) {
e.printStackTrace();
}
}
. . .
public ServerFrame() {
super();
try {
UnicastRemoteObject.exportObject((Remote) this);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
. . . // bulding the ServerFrame GUI
. . .
public void share(BufferedImage image) throws RemoteException {
label.setIcon(new ImageIcon(image));
}
}
---------------
//最后是这样的客户端:存在,我想在桌面的ClientFrame按钮在点击这个按钮后在客户端和服务器之间共享。
public class ClientFrame extends JFrame implements Remote,Serializable{
IServer serve;
...
public ClientFrame() {
super();
try {
serve = (IServer) Naming.lookup("test");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
. . . // codes for building client gui
final JButton shareDesktopButton = new JButton();
shareDesktopButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
try {
BufferedImage screenShot = new Robot()
.createScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
label.setIcon(new ImageIcon(screenShot));
serve.share(screenShot);
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
. . .
}
// but there is a problem that causes these exceptions that I can not understand it. Please help me to complete my project.
非常感谢
例外:
java.rmi.MarshalException: error marshalling arguments; nested exception is:
java.io.NotSerializableException: java.awt.image.BufferedImage
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at server.ServerFrame_Stub.share(Unknown Source)
at client.ClientFrame$1.mouseClicked(ClientFrame.java:86)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.NotSerializableException: java.awt.image.BufferedImage
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at sun.rmi.server.UnicastRef.marshalValue(Unknown Source)
... 24 more
对,没错。非常感谢 – samuel 2010-02-23 21:08:47