java 未找到 rmic 错误类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12336224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rmic error class not found
提问by WannaCSharp
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:
我首先阅读 java,我将尝试展示如何使用 RMI 的现成烘焙代码。这些是类:
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.
然后我将 .java 和 .class 文件放在 c:\RMI 中。运行它时,它说找不到 MyRemoteImpl 类,即使我从同一目录运行。我怎样才能解决这个问题?谢谢。
EDIT: The error appear when I try to run this command
编辑:当我尝试运行此命令时出现错误
rmic MyRemoteImpl
回答by Renato Bueno
The comments of BuddingProgrammer worked for my as well.
BuddingProgrammer 的评论也对我有用。
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
例如,如果您的类位于文件夹 C:\users\renato\workspace\ADI\src\semana5 中,您应该转到上一层:C:\users\renato\workspace\ADI\src 并且您应该像这样编译:javac semana5\Calculadora.java
To run RMI you should type the following command in C:\users\renato\workspace\ADI\src
:
要运行 RMI,您应该在 中键入以下命令C:\users\renato\workspace\ADI\src
:
rmic semana5.CalculadoraImp
回答by MasterJoe2
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.
我有相同的代码,但在 Maven 项目中。第一步是编译所有代码mvn compile
。请注意,所有已编译的类(例如本例中的 MyRemoteImpl)都存储在 Maven 项目的目标文件夹中。
The next step is to cd to \target\classes
folder.
下一步是cd to \target\classes
文件夹。
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
.
最后,使用目标类的完全限定名称运行 rmic 命令,即使用类的包 - rmic my_package1.my_sub_package.MyRemoteImpl
。
回答by user207421
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.
如果您不是从包含 .class 文件的目录中运行此命令,那么您应该这样做,假设您发布的内容没有包语句。如果有包语句,您应该从包含最高级别包的目录运行此命令。
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.
否则您的 CLASSPATH 环境变量有问题。它至少应该包含“.”,可能还有其他一些东西。作为一种解决方法,您可以采用 Isaac 的建议,但是只有在编译和执行时才会遇到相同的问题。
回答by Ramzi Komati
- 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 :)
- 创建一个文件夹命名它
HelloServer
package HelloServer;
在每个类的顶部添加一个 this 包,MyRemote
并且MyRemoteImpl
.- 打开 cmd 并从包含该文件夹的目录中写入
javac HelloServer/MyRemote.java
和。javac HelloServer/MyRemoteImpl.java
HelloServer
rmic HelloServer.MyRemoteImpl
从包含HelloServer
文件夹的目录写入。- 你现在应该有一个
MyRemoteImpl_stub.class
,你可以有一个美好的一天:)
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.
PS:包名与 RMI 或类内使用的任何对象不同,这一点很重要。否则你会发生物体碰撞。
回答by user825628
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.
由于我遇到了同样的问题并且这些答案都没有帮助我,我将添加对我有用的内容。我在包含 MyRemoteImpl.java 的目录中运行 rmic MyRemoteImpl 并得到相同的错误。当我在包含 MyRemoteImpl.class 的目录中运行 rmic MyRemoteImpl 时,它工作并创建了 MyRemoteImpl_Stub.class。我不需要设置 CLASSPATH。
回答by BuddingProgrammer
if the class is located in C:/../src/Assign/implement.class then
如果类位于 C:/../src/Assign/implement.class 那么
- change your directory in cmd by entering:- cd C:/....../src
- run the command:- rmic Assign.implement.class
- 通过输入以下命令更改 cmd 中的目录:- cd C:/....../src
- 运行命令:- rmic Assign.implement.class
It worked for me
它对我有用
回答by Isaac
Try adding:
尝试添加:
-classpath .
By default, rmic's classpath is derived from the CLASSPATH environment variable (thanks EJP for the correction).
默认情况下,rmic 的类路径源自 CLASSPATH 环境变量(感谢 EJP 的更正)。