gpt4 book ai didi

java - 引导 RMI

转载 作者:行者123 更新时间:2023-12-02 05:05:28 27 4
gpt4 key购买 nike

我已经遵循了几个有关运行 RMI 应用程序的教程。但是,我似乎无法使其工作,因为我一直陷入相同的错误:ClassNotFoundException。我知道这个错误意味着我将文件放在了错误的位置,但我尝试将 Client.class 放在任何地方,但没有成功。

这是 Bootstrap.java:

package mobility_server;
import java.io.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;

public interface Bootstrap extends Remote {
Runnable getClient() throws RemoteException;
}

这是Client.java:

package mobility_server;
import java.io.*;

public class Client implements Runnable, Serializable {
public void run() {
System.out.println("Io sono il Client caricato dinamicamente");
try {
System.out.println("ora scrivo su prova.txt");
BufferedWriter out = new BufferedWriter(new FileWriter("prova.txt"));
out.write("ciaociao");
out.close();
System.out.println("Ora leggo da prova.txt");
BufferedReader in = new BufferedReader(new FileReader("prova.txt"));
String c = in.readLine();
System.out.println(c);
in.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

这是我的政策:

grant {
// Allow everything for now
permission java.security.AllPermission;
};

这是 BootstrapServer.java:

package mobility_server;
import java.io.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;

class BootstrapServer extends UnicastRemoteObject implements Bootstrap {
public BootstrapServer() throws RemoteException {}

public Runnable getClient() {
return new Client();
}

public static void main(String[] args) throws Exception {
Bootstrap server = new BootstrapServer();
System.out.println("Il Server di Bootstrap e' stato lanciato.");
Naming.rebind("//:3333/BootServer",server);
System.out.println("Il Server di Bootstrap e' stato registrato nel registro RMI.");
}
}

这是 RMIClientBootstrap.java:

package mobility_server;
import java.io.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
//Bootstrap bootstrap = (Bootstrap) Naming.lookup("//157.127.135.126:3333/BootServer");

public class RMIClientBootstrap {
public static void main(String[] args) throws Exception {

System.out.println("Il client si accinge a fare la lookup");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
for (int i = 0; i < args.length; i++) {
try {
String host = args[i];
int port = Registry.REGISTRY_PORT;
if (i+1 < args.length) {
try {
port = Integer.parseInt(args[i + 1]);
i++;
}
catch (NumberFormatException e) {
// next arg isn't a port number
}
}
Registry registry = LocateRegistry.getRegistry(host,port);
// if the try construct failed we have in the variable port
//the standard port number 1099 from Registry.REGISTRY_PORT
System.out.println("Contenuto del registro RMI presso "+host+":"+port);
String list[] = registry.list();
Bootstrap bootstrap = (Bootstrap)registry.lookup(list[0]);
// remember that the lookup method return a remote reference, that is a stub.
System.out.println(".\tnome="+list[0]+"\n\tstub="+bootstrap);

System.out.println("Il client si accinge ad invocare il metodo getClient() del server ricavato dal registroRMI.");
Runnable client = bootstrap.getClient();
System.out.println("Il client ha ottenuto un'istanza della classe Client e la lancia");
client.run();
}//TRY
catch (RemoteException e) {
System.out.println("SONO NEL CATCH " + e.getMessage());
e.printStackTrace();
}
}//FOR
}
}

我制作了这两个脚本:

服务器.sh:

#!/bin/bash

rmi=$(pidof "rmiregistry")

if [ `pidof "rmiregistry" | cut -d" " -f1` ]; then
kill -9 $rmi
fi

cd ~/javarmi/mobility_server/
javac Bootstrap.java Client.java BootstrapServer.java

cp Client.class ~/public_html/common/mobility_server/
rm Client.class

cd ~/javarmi/
rmic -d ~/public_html/common/ mobility_server.BootstrapServer

cd ~/javarmi/
unset CLASSPATH
rmiregistry 3333 -J-Djava.rmi.server.codebase=http://192.168.1.68/public_html/common/ &

cd ~/javarmi/
java -Djava.security.policy=/home/emanuele/javarmi/mobility_server/policy - Djava.rmi.server.codebase=http://192.168.1.68/public_html/common/ mobility_server.BootstrapServer

Client.sh:

#!/bin/bash

cd ~/javarmi/mobility_server/
javac Bootstrap.java RMIClientBootstrap.java

cd ~/javarmi/
java -Djava.security.policy=/home/emanuele/javarmi/mobility_server/policy mobility_server.RMIClientBootstrap 192.168.1.68 3333

最佳答案

多么奇怪的困惑啊。试试这个:

#!/bin/bash    
rmi=$(pidof "rmiregistry")
if [ `pidof "rmiregistry" | cut -d" " -f1` ]; then
kill -9 $rmi
fi

cd ~/javarmi
javac mobility_server/Bootstrap.java mobility_server/Client.java mobility_server/BootstrapServer.java
rmic mobility_server.BootstrapServer
cp mobility_server/Client.class mobility_server/BootstrapServer_Stub.class ~/public_html/common/mobility_server/

(unset CLASSPATH; cd /; rmiregistry 3333)&

java -Djava.security.policy=/home/emanuele/javarmi/mobility_server/policy - Djava.rmi.server.codebase=http://192.168.1.68/public_html/common/ mobility_server.BootstrapServer

具体:

  1. 您将从 BootstrapServer 查找 Client.class 的位置删除它。
  2. 同样,您也没有将 stub 类放在服务器将查找的位置。
  3. 当您只需要在服务器 JVM 上设置代码库属性时,您在注册表 JVM 上设置的代码库属性不正确。

关于java - 引导 RMI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27830859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com