我一直在使用 RMI 开发一个小程序,我想尝试将我的客户端作为一个 jar 进行测试(也许将它放在另一台计算机上,使用我自己的 localhost 作为连接的 ip)但每次我在 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 中编译工作?
这是我整个项目的代码
客户端.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);
}
}
服务器.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();
}
}
非常感谢!