我一直在使用RMI开发一个小程序,我想尝试将我的客户端测试为一个jar(也许把它放在不同的计算机上使用我自己的localhost作为连接的ip)但每次我使用VM选项在Intellij中编译它:
-Djava.security.manager -Djava.security.policy=src/test.policy
这是我的test.policy:
grant {
permission java.security.AllPermission;
permission java.net.SocketPermission "*:1024-65535",
"connect,accept,resolve";
};
当我在Intellij中编译它时,在我放入主机(localhost)之后,它给了我标题中的消息
error unmarshalling return; nested exception is: java.lang.ClassNotFound: ServerImplements_Stub
我问我如何让它在jar中工作,甚至在Intellij中编译才能工作?
这是我整个项目的代码
Client.java
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Client extends JFrame {
JTextField textField = new JTextField();
JPanel p = new JPanel(new BorderLayout());
JButton b = new JButton("Send message");
RemoteInterface s;
String name;
JTextArea textArea = new JTextArea();
public Client() {
super("Chat client v1.0 by Jurko Guba");
setSize(250, 250);
setLocation(300, 300);
getContentPane().add(p);
p.add(textField, BorderLayout.NORTH);
p.add(textArea, BorderLayout.CENTER);
p.add(b, BorderLayout.SOUTH);
try {
textArea.setText(s.returnChat());
} catch (Exception e) {System.out.println(e);}
try {
String ipp = JOptionPane.showInputDialog("Enter IP Address to Connect");
name = JOptionPane.showInputDialog("Enter chat username ie. Jurko");
String ip = "rmi://" + ipp + "/RMIAPPLICATION";
s = (RemoteInterface)Naming.lookup(ip);
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, exp.getMessage());
}
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
s.addChatMessage(name + ": " + textField.getText());
textArea.setText(s.returnChat());
textArea.repaint();
System.out.println("Client message added: "+textField.getText() + " under the name of: "+name);
} catch (Exception epx) {System.out.println(epx.getMessage());}
}
});
}
public static void main(String args[]) {
System.setSecurityManager(new SecurityManager());
System.setProperty("java.security.policy", "file:test.policy");
System.out.println(java.security.Policy.getPolicy());
Client c = new Client();
c.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
c.setVisible(true);
}
}
Server.java
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.Naming;
/**
* Created with IntelliJ IDEA.
* User: jurkoguba
* Date: 2013-09-10
* Time: 9:14 PM
* To change this template use File | Settings | File Templates.
*/
public class Server {
public static void main (String args[]) {
try {
System.setSecurityManager(new SecurityManager());
System.setProperty("java.security.policy", "file://test.policy");
ServerImplements s = new ServerImplements();
Naming.rebind("RMIAPPLICATION", s);
System.out.println("Server has been started!");
JFrame f = new JFrame("Server");
f.setSize(250, 250);
f.setLocation(100, 100);
f.getContentPane().add(s.textArea);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
RemoteInterface.java
import javax.swing.*;
import java.rmi.*;
/**
* Created with IntelliJ IDEA.
* User: jurkoguba
* Date: 2013-09-10
* Time: 8:56 PM
* To change this template use File | Settings | File Templates.
*/
public interface RemoteInterface extends Remote {
public void addChatMessage(String message) throws Exception;
public String returnChat() throws Exception;
}
ServerImplements.java
import javax.swing.*;
import java.rmi.Remote;
import java.rmi.server.UnicastRemoteObject;
/**
* Created with IntelliJ IDEA.
* User: jurkoguba
* Date: 2013-09-10
* Time: 9:01 PM
* To change this template use File | Settings | File Templates.
*/
public class ServerImplements extends UnicastRemoteObject implements RemoteInterface {
public ServerImplements() throws Exception {
super();
}
final JTextArea textArea = new JTextArea("Brand new chat!");
public void addChatMessage(String message) {
textArea.append('\n'+message);
System.out.println("Message appended: "+ message);
textArea.repaint();
}
public String returnChat() throws Exception {
return textArea.getText();
}
}
非常感谢!