我正在编写一个简单的 rmi 应用程序,但遇到了问题。
如果我在同一目录中运行注册表,它就可以工作;但如果我更改运行注册表的目录,则不会。
注册表通常可以在另一台主机上工作,但只有目录的更改才能停止其功能。
我花了 3 天的时间来解决这个问题,但没有解决方案,我还更改了代码库参数的每个可能的配置,但什么也没改变。
我用目录描述一下情况和代码:
文件服务器.java:
`package testrmi2;
import java.rmi.*;
public interface fileserver extends Remote {
public void scrivifile(String nomefile, String arg) throws RemoteException;
}
` fileserverimpl.java:
package testrmi2;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class fileserverimpl extends UnicastRemoteObject implements fileserver{
public fileserverimpl() throws RemoteException {
super();
}
public void scrivifile(String nomefile, String arg) throws RemoteException {
try {
FileWriter myfile = new FileWriter(nomefile);
myfile.write(arg);
myfile.close(); }
catch (Exception e) {System.out.println(e);}
}
public static void main (String arg[]) {
try {
fileserverimpl s = new fileserverimpl();
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String codebase = System.getProperty("classpath");
System.out.println("Trying to access code base: " + codebase+"\n\nuse is "+System.getProperty("useCodebaseOnly"));
Naming.rebind("//127.0.0.1:2005/fileserverimpl", s);
System.out.println("Server attivato.");
} catch (Exception e) {System.out.println("errore inizializzazione server\n\n"+e.getMessage()+"\n\n\n");
}}}
客户端.java:
package testrmi2;
import java.rmi.*;
import java.io.*;
public class client {
public static void main (String arg[]) {
fileserver myserver;
String nomefile=" ";
String testo=" ";
System.out.println("Scrivi il nome del file");
nomefile=ReadString();
System.out.println("Scrivi il testo");
testo=ReadString();
try {
myserver = (fileserver) Naming.lookup("//127.0.0.1:2005/fileserverimpl");
myserver.scrivifile(nomefile, testo);
} catch (Exception e) {System.out.println(e);}
}
public static String ReadString() {
BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));
String s=" ";
try{
s=stdIn.readLine();
}
catch(IOException e) {System.out.println(e.getMessage()); }
return s;
}
}
策略文件是:
grant {
// Allow everything for now
permission java.security.AllPermission;
};
所有这些文件都在目录中:
/Users/franco/Desktop/prova
为了编译它,我进入/Users/franco/Desktop/prova 目录并在终端中执行:
javac -cp . -d . *.java
rmic rmic testrmi2.fileserverimpl
jar cvf testrmi2.jar testrmi2/fileserver.class testrmi2/fileserverimpl_Stub.class
在另一个终端中使用以下命令但在另一个目录中运行注册表后:
export classpath=""
rmiregistry 2005 &
最后,我将在/Users/franco/Desktop/prova 目录中运行 filesereveimpl.class 与终端一起运行并写入:
java -classpath /Users/franco/Desktop/prova/ -Djava.rmi.server.codebase=file:/Users/franco/Desktop/prova/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
但结果是:
Trying to access code base: null
use is null
errore inizializzazione server
RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: testrmi2.fileserverimpl_Stub
我还尝试在本地网络服务器 xampp 上公开他的 jar 并尝试使用以下命令运行:
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2/ -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
或者用:
java -classpath . -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2.jar -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &
但我得到了相同的结果。
最佳答案
尝试在执行 rmregistry 之前设置类路径 var:
export classpath="/Users/franco/Desktop/prova/"
rmiregistry 2005 &
关于java - rmi java.lang.ClassNotFoundException : testrmi2. fileserverimpl_Stub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19298847/