简体   繁体   中英

rmi java.lang.ClassNotFoundException: testrmi2.fileserverimpl_Stub

I'm programming a simple rmi application, and I have a problem.

If I run the registry in the same directory, it works; but if I change the directory from which I run the registry, it does not.

The registry would work generically from another host, but only the change of directory stop his functionality.

I'm working on this problem by 3 days without solution, I change also every possible configuration of the codebase parameter but nothing.

I describe the situation and code with the directory:

fileserver.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");

}}}

client.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;
   }
}

and the policy file is:

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

all this files are on the directory:

/Users/franco/Desktop/prova

to compiling it I goes in the /Users/franco/Desktop/prova directory and do in terminal :

javac -cp . -d . *.java
rmic rmic testrmi2.fileserverimpl
jar cvf testrmi2.jar testrmi2/fileserver.class testrmi2/fileserverimpl_Stub.class

after I run registry in another terminal with the following commands but in another directory:

export classpath=""
rmiregistry 2005 &

Finally I would to run the filesereveimpl.class goes with terminal in the /Users/franco/Desktop/prova directory and write :

java -classpath /Users/franco/Desktop/prova/ -Djava.rmi.server.codebase=file:/Users/franco/Desktop/prova/testrmi2.jar  -Djava.security.policy=testrmi2/policy testrmi2.fileserverimpl &

But the results are:

    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

I also try to public he jar on a local webserver xampp and try to run with the following:

java -classpath .  -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2/  -Djava.security.policy=testrmi2/policy  testrmi2.fileserverimpl &

or with :

java -classpath .  -Djava.rmi.server.codebase=http://127.0.0.1/testrmi2.jar  -Djava.security.policy=testrmi2/policy  testrmi2.fileserverimpl &

but I have the same results.

Try to set the classpath var before to execute the rmregistry:

export classpath="/Users/franco/Desktop/prova/"
rmiregistry 2005 &

There are three cases.

  1. You got that exception in the server when exporting/constructing the remote object. Solution: run rmic to generate the stub.
  2. You got it in the server when binding/rebinding. Solution: make the stub class available to the Registry's CLASSPATH, or run the Registry in the server JVM via LocateRegistry.createRegistry().
  3. You got it in the client, in lookup(). Solution: make the stub class available to the client's CLASSPATH.

These also apply to the remote interface itself, and any application classes it depends on, and so on recursively until closure.

Solution for stubs to all three: take the measures outlined in the Javadoc preamble to UnicastRemoteObject, so you don't need a stub at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM