java Rmi:未绑定异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17644027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rmi: Not Bound exception
提问by J.S
I am pretty new in Java RMI and i am having a problem of Not Bound Exception. I know there are a lot of posts here but anything i have tried the last hours did not help really..
我是 Java RMI 的新手,我遇到了 Not Bound Exception 的问题。我知道这里有很多帖子,但是我过去几个小时尝试过的任何内容都没有帮助。
Here is the code of the server and the client. Any help is good, thanks in advance.
这是服务器和客户端的代码。任何帮助都是好的,提前致谢。
One last thing, the exception is found when i am running Master, when i am running server no errors are found.
最后一件事,当我运行 Master 时发现异常,当我运行服务器时没有发现错误。
Master:
掌握:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.util.Scanner;
import java.rmi.registry.LocateRegistry;
public class Master {
private ITaskBag1 tb;
private String name = "rmi://" + "127.0.0.1" +":"+1099+"/TaskBagServer";
private int rescount = 0,listcount=0;
private boolean finished = false;
private ArrayList<String> tasks = new ArrayList<String>();
private Random rand;
private double rkey;
public static void main(String[] args){
//pithano lathos
try {
Master m = new Master(args);
} catch (Exception exc) { }
}
Master (String[] args){
rand = new Random();
String url="";
String s1="",s2="",s3="",myLine="",key="";
File myFile=null;
File imgFile = null;
String myFileName="";
String myFileName2="";
StringTokenizer st;
BufferedReader br=null;
int numoftokens = 0;
int endframe = 0;
int degrees = 0;
int eikona = 001;
int i;
double scale = 1.0;
byte[] imageInByte = null;
imageData imdat = null;
imageResult imres = null;
Boolean registered = false;
//Register to ITaskbag
try { //try 1
//try to register
tb = (ITaskBag1) Naming.lookup(name);
}
}
}
Server:
服务器:
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class TaskBagServer{
public static void main(String args[]) {
final int DEFAULT_PORT = 1099;
String sport="";
int port=DEFAULT_PORT;
String connectionstring="//127.0.0.1/TaskbagServer";
//String porterror = "Invalid port specified, " + DEFAULT_PORT + " assumed";
try {
//LocateRegistry.createRegistry(1099);
// connectionstring = "//127.0.0.1:"+port+"/TaskbagServer";
java.rmi.registry.LocateRegistry.createRegistry(1099);
} catch (RemoteException ex) {
Logger.getLogger(TaskBagServer.class.getName()).log(Level.SEVERE, null, ex);
}
try {
TaskBag1 server = new TaskBag1();
Naming.rebind("connectionstring",server);
System.out.println("Taskbag Server Bound");
}
}
}
回答by Qben
I think the problem is that you quote the connectionstring
variable in your Naming.rebind()
call in the server. This will set the URL to connectionstring
and not what you really want.
我认为问题在于您在服务器connectionstring
的Naming.rebind()
调用中引用了变量。这会将 URL 设置为connectionstring
而不是您真正想要的。
回答by Klumbe
what are you doing here?
你在这里做什么?
try {
TaskBag1 server = new TaskBag1();
Naming.rebind("connectionstring",server);
System.out.println("Taskbag Server Bound");
}
"connectionstring" will be the URI to rebind...you should write it without the "", because you want to refer to a String.
By the way: you should use the prefix "rmi:" in that String.
"connectionstring" 将是要重新绑定的 URI...你应该在没有 "" 的情况下编写它,因为你想引用一个字符串。
顺便说一句:您应该在该字符串中使用前缀“rmi:”。
回答by user207421
[After you remove the quotes]: Check your spelling. You have both TaskBagServer and TaskbagServer.
[删除引号后]:检查拼写。您同时拥有 TaskBagServer 和 TaskbagServer。
I use the class name of the remote interface as the bind name, i.e. in this case ITaskBag1.class.getName(). It eliminates this kind of problem with typos, as if you get it wrong it doesn't compile, and it also solves the uniqueness problem.
我使用远程接口的类名作为绑定名,即在本例中为 ITaskBag1.class.getName()。它消除了这种打字错误的问题,如果你弄错了它不会编译,它也解决了唯一性问题。