rmic error class not found

老子叫甜甜 提交于 2020-06-26 16:24:10

问题


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:


  1. Create a folder name it HelloServer
  2. Add a this package package HelloServer; on the top of each class, MyRemote and MyRemoteImpl.
  3. Open cmd and write javac HelloServer/MyRemote.java and javac HelloServer/MyRemoteImpl.java from the directory that contain the HelloServer folder.
  4. Write rmic HelloServer.MyRemoteImpl from the directory that contain the HelloServer folder.
  5. 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

  1. change your directory in cmd by entering:- cd C:/....../src
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!