我遇到了我正在構建的Java RMI系統的問題。當我嘗試連接時,我不斷收到UnknownHostException。對於主機名稱,我嘗試了我的IPv4地址,IPv6地址,「本地主機」以及其他一些內容。我也試着隨機輸入文字,看看是否有區別。沒有;我得到了完全相同的錯誤。 下面是一些代碼:RMI UnknownHostException
public class GUI extends JFrame implements ActionListener, WindowListener
{
public static GUI Global;
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
Global = new GUI();
//Debugging
System.out.println(Global.Client.getServerMessage());
}
public ClientIO()
{
System.setProperty("java.security.policy", "file:./bin/settings.policy");
/*if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}*/
System.out.println("Enter the Server IP adress (The IP adress of the machine that the server is running on)");
try
{
ClientIO.URL = new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*try {
URL = java.net.InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
public String getServerMessage()
{
String szRemoteAdress = URL+"/"+WebConstants.SERVICE_NAME;
try
{
Registry registry = LocateRegistry.getRegistry(szRemoteAdress);
EotGRemote stub = (EotGRemote) registry.lookup("EotGRemote");
String response = stub.getTextyness();
System.out.println("response: " + response);
}
catch (RemoteException | NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public class GlobalNetIO implements Runnable
{
public static GlobalNetIO IO;
ServerSocket m_MainServerSocket;
ArrayList<Socket> m_Sockets;
ArrayList<UserNetIO> m_UserInterfaces;
public GlobalNetIO()
{
makeRMI();
try
{
m_MainServerSocket = new ServerSocket(1111);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Define an ArrayList of the Socket
m_Sockets = new ArrayList<Socket>();
}
public void makeRMI()
{
//I don't know what this means; I'm just going to accept it
System.setProperty("java.security.policy","file:./bin/settings.policy");
//Might as well...
System.setProperty("java.rmi.server.codebase", "./");
/*if (System.getSecurityManager() == null)
System.setSecurityManager (new RMISecurityManager());*/
try
{
EotGRemote IO2 = new EotGRemoteIO();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind(WebConstants.SERVICE_NAME, IO2);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public interface EotGRemote extends Remote
{
public String getTextyness() throws RemoteException;
}
package com.eotg.WebInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import com.eotg.WebUtils.*;
public class EotGRemoteIO extends UnicastRemoteObject implements EotGRemote
{
protected EotGRemoteIO() throws RemoteException
{
}
@Override
public String getTextyness() {
// TODO Auto-generated method stub
return "It worked!";
}
}
而這裏的錯誤消息:
java.rmi.UnknownHostException: Unknown host: foo bar/Eo Nova; nested exception is:
java.net.UnknownHostException: foo bar/Eo Nova
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at com.eotg.Client.ClientIO.getServerMessage(ClientIO.java:57)
at com.eotg.Client.GUI.main(GUI.java:34)
Caused by: java.net.UnknownHostException: foo bar/Eo Nova
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 7 more
請添加**確切**錯誤消息與呼叫跟蹤到您的文章。 –