问题
I'm reading head first java and I'm about to try the ready baked codes that shows how to use RMI. These are the classes:
The remote interface
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
The remote implementation
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello() {
return "Server Says,Hello";
}
public MyRemoteImpl() throws RemoteException { }
public static void main(String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Then I placed the .java and the .class file in c:\RMI. When running it, it says MyRemoteImpl class not found even though I'm running from the same directory. How can i fix this? Thanks.
EDIT: The error appear when I try to run this command
rmic MyRemoteImpl
回答1:
The comments of BuddingProgrammer worked for my as well.
For instance if your classes are in folder C:\users\renato\workspace\ADI\src\semana5 you should go to one level above: C:\users\renato\workspace\ADI\src and you should compile like this: javac semana5\Calculadora.java
To run RMI you should type the following command in C:\users\renato\workspace\ADI\src
:
rmic semana5.CalculadoraImp
回答2:
- Create a folder name it
HelloServer
- Add a this package
package HelloServer;
on the top of each class,MyRemote
andMyRemoteImpl
. - Open cmd and write
javac HelloServer/MyRemote.java
andjavac HelloServer/MyRemoteImpl.java
from the directory that contain theHelloServer
folder. - Write
rmic HelloServer.MyRemoteImpl
from the directory that contain theHelloServer
folder. - You should have now a
MyRemoteImpl_stub.class
and you can have a nice day :)
PS: It is important that the package name is different than RMI or any object used inside the class. Otherwise you will have object collision.
回答3:
Since I had the same problem and none of these answers helped me, I'll add what worked for me. I was running rmic MyRemoteImpl in the directory that contained MyRemoteImpl.java and got the same error. When I ran rmic MyRemoteImpl in the directory that contained MyRemoteImpl.class, it worked and created MyRemoteImpl_Stub.class. I did not need to set CLASSPATH.
回答4:
if the class is located in C:/../src/Assign/implement.class then
- change your directory in cmd by entering:- cd C:/....../src
- run the command:- rmic Assign.implement.class
It worked for me
回答5:
If you're not running this command from the directory containing the .class file, you should be, assuming there is no package statement as per what you posted. If there's a package statement you should be running this command from the directory that contains the highest-level package.
Otherwise there's something wrong with your CLASSPATH environment variable. It should at least contain ".", probably some other stuff as well. As a workaround you can adopt Isaac's suggestion but then you'll only get the same problem when you come to compile and execute.
回答6:
I have the same code, but in a Maven project. First step is to compile all the code with mvn compile
. Note that all compiled classes (ex.MyRemoteImpl in this case) are stored in the target folder in your maven project.
The next step is to cd to \target\classes
folder.
Finally, run the rmic command with the fully qualified name of the target class, i.e with the packages of the class - rmic my_package1.my_sub_package.MyRemoteImpl
.
回答7:
Try adding:
-classpath .
By default, rmic's classpath is derived from the CLASSPATH environment variable (thanks EJP for the correction).
来源:https://stackoverflow.com/questions/12336224/rmic-error-class-not-found