我正在RMI中開發一個簡單的電話簿應用程序。我能夠啓動RMI註冊表,併爲實現c1ass生成存根類。現在我已經在我的cmd提示符下使用命令Java PhoneBookServer啓動了服務器。下一步是啓動客戶端,所以在我啓動客戶端後出現以下錯誤!客戶端和服務器程序都在一個文件夾中客戶端如何在Java RMI中連接到服務器?
我用到目前爲止了客戶端和服務器的代碼如下所示
import java.rmi.*;
import java.rmi.server.*;
public class PhoneBookServer {
public static void main (String[] args){
/*Create and install a security manager
SecurityManager appsm = System.getSecurityManager();
if(appsm==null){
System.setSecurityManager(new RMISecurityManager());
}*/
System.out.println("Server is started");
try
{
//create PhoneBookImpl
PhoneBookImpl Pb=new PhoneBookImpl();
Naming.rebind("rmi://127.0.0.1:1099/PhoneBook", Pb);
}
catch(Exception e)
{
System.out.println("Exception is:" +e);
}
}
}
客戶端程序
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class PhoneBookClient {
public static void main (String[] args) throws Exception
{
/*Create and install a security manager
SecurityManager appsm = System.getSecurityManager();
if(appsm==null){
System.setSecurityManager(new RMISecurityManager());
}*/
String name,number,total,choice,id;
String ch;
PhoneBook pb=(PhoneBook)Naming.lookup("rmi://127.0.0.1:1099"+"/PhoneBook");
Scanner in=new Scanner(System.in);
System.out.println("1.Enter new record /n");
System.out.println("2.Look up record /n");
System.out.println("3.Delete record /n");
System.out.println("Enter your option");
ch=in.nextLine();
if(ch.equals("1")){
do{
System.out.println("Enter unique id:");
id=in.nextLine();
System.out.println("Enter name:");
name=in.nextLine();
System.out.println("Enter phone number:");
number=in.nextLine();
total=name+" "+number;
pb.new_record(id,total);
System.out.println("Enter 'q' to quit or enter 'p' to proceed");
choice=in.nextLine();
}while(!choice.equals("q"));
}
if(ch.equals("2")){
do{
System.out.println("Enter id to look up a record"+" enter 'q' to quit");
id=in.nextLine();
String record=pb.lookup_record(id);
System.out.println("The record is" +record);
}while(!id.equals("q"));
}
if(ch.equals("2")){
do{
System.out.println("Enter id to delete a record"+" enter 'q' to quit");
id=in.nextLine();
pb.lookup_record(id);
System.out.println("The record is deleted");
}while(!id.equals("q"));
}
}
}
以前我得到異常:
Connection refused to the host127.0.0.1 access denied.
所以我安裝安全mnager在我的cl客戶和服務器程序。現在我得到這種新的例外。我該如何解決這個問題。
您可以通過點擊上面問題中的「enter image description」在cmd提示符中看到錯誤 –
什麼新的異常?什麼問題?發佈例外和代碼*在這裏*大多數人不會打擾跟隨鏈接,並且該問題沒有永久的價值,除非它包含所有相關材料。 – EJP
http://stackoverflow.com/questions/2427473/java-rmi-accesscontrolexception-access-denied –