Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

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+"

use 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

"+e.getMessage()+"


");

}}}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...