RMI - 在 Eclipse 上运行客户端代码会抛出 MarshalException,而从控制台执行则工作正常

发布于 2024-09-26 02:21:25 字数 2320 浏览 3 评论 0原文

我编写了简单的 RMI 代码来了解它的工作原理。 虽然编译 java 代码一切正常,但 rmic 它并运行 rmiregistry 并从控制台运行客户端代码工作正常,但是当我选择在 Eclipse 的客户端代码上“作为 Java 应用程序运行”时,它会抛出 MarshallException。

我的猜测是 Eclipse 正在使用不同的版本进行编译,根据:

java.rmi.MarshalException

我已经检查了 Eclipse 编译器设置,它显示 1.6。 (右键单击 Project Explorer 并选择“project facets”,然后您将看到 Eclipse 正在编译 java 文件的版本)

我的 Java 是 1.6 所以无论从控制台和 Eclipse 编译代码都应该导致1.6版本编译了类文件。 (javap -verbose | grep -i version 显示版本 50 相当于 1.6

有没有人遇到过同样的问题并对此有解释?

可能没有必要,但工作代码如下。

客户端代码

package com.masatosan.remote.client;

import java.rmi.Naming;
import java.rmi.RemoteException;

import com.masatosan.remote.server.MyRemoteServer;

public class MyRemoteClient  {

    public static void main(String[] args) {
        new MyRemoteClient().go();
    }

    public void go() {
        try {
            MyRemoteServer service = (MyRemoteServer) Naming.lookup("rmi://127.0.0.1/MyFirstRemoteService");
            String s = service.sayHello();

            System.out.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

服务器代码

package com.masatosan.remote.server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteServerImp extends UnicastRemoteObject implements MyRemoteServer {

    public static void main(String[] args) {
        try {
            MyRemoteServer service = new MyRemoteServerImp();
            Naming.rebind("MyFirstRemoteService", service);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public MyRemoteServerImp() throws RemoteException {}

    @Override
    public String sayHello() throws RemoteException {
        return "Hi I'm server!";
    }
}//

存根的服务器代码接口

package com.masatosan.remote.server;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemoteServer extends Remote {

    public String sayHello() throws RemoteException;
}

I've wrote simple RMI code to learn how it works. While everything works fine compiling java code, rmic it and run rmiregistry and run client code from console works fine, but when I choose to "run as Java application" on the client code from Eclipse, it throws MarshallException.

My guess is somewhat Eclipse is compiling with different version according to:

java.rmi.MarshalException

I've checked the Eclipse compiler setting and it says 1.6.
(right click on Project Explorer and choose "project facets" then you will see what version the Eclipse is compiling the java files)

My Java is 1.6 so regardless of compiling the code from console and Eclipse should result in 1.6 version complied class files. (javap -verbose | grep -i version brings up version 50 which is equivalent to 1.6 )

Has anyone encountered same issue and has explanation for this?

Likely not necessary but the working code is below.

Client code

package com.masatosan.remote.client;

import java.rmi.Naming;
import java.rmi.RemoteException;

import com.masatosan.remote.server.MyRemoteServer;

public class MyRemoteClient  {

    public static void main(String[] args) {
        new MyRemoteClient().go();
    }

    public void go() {
        try {
            MyRemoteServer service = (MyRemoteServer) Naming.lookup("rmi://127.0.0.1/MyFirstRemoteService");
            String s = service.sayHello();

            System.out.println(s);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

Server code

package com.masatosan.remote.server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteServerImp extends UnicastRemoteObject implements MyRemoteServer {

    public static void main(String[] args) {
        try {
            MyRemoteServer service = new MyRemoteServerImp();
            Naming.rebind("MyFirstRemoteService", service);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public MyRemoteServerImp() throws RemoteException {}

    @Override
    public String sayHello() throws RemoteException {
        return "Hi I'm server!";
    }
}//

Server code interface for stub

package com.masatosan.remote.server;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemoteServer extends Remote {

    public String sayHello() throws RemoteException;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-10-03 02:21:25

有问题的版本不是 JRE 的版本,而是被调用的类的版本。这意味着您的客户端正在 eclipse 中调用一组不同的 jar。一般来说,如果 Eclipse 和控制台给出不同的结果,则几乎总是类路径问题。

检查项目的“Java 构建路径”部分以及所有相关项目,以确保一切都匹配。请留意包含“com.masatosan.remote.server.MyRemoteServer”的 jar,我相信这是存在版本问题的 jar。 eclipse 指向的 jar 可能不同和/或过时。以下是您项目的构建路径部分中需要检查的一些地方

1) 确保您正确使用“项目”选项卡。

2) 检查以确保“libraries”选项卡上的所有 jar 与在控制台上编译时相同

3) 确保“projects”选项卡应包含的 jar 均不在“libraries”中选项卡。我见过很多人因为 Eclipse 使用的 jar 包而感到烦恼,而这些 jar 包只有从命令行编译时才会更新。

4) 使用“订购和导出”选项卡。如果某个东西位于其他东西之上,它就会首先加载。最先加载的版本是使用的版本。如果您的命令行应用程序和 Eclipse 项目都有相同的 jar,但以不同的顺序包含它们,您可能会遇到这些问题。

5)一些jar可以通过将它们包含在JREs目录中来直接安装到JRE中。这种情况极不可能发生,但您可以检查 eclipse 正在使用的 JRE,并确保它与控制台相同。

祝你好运,类路径问题非常棘手。

The version in question is not the version of the JRE but the version of the classes being called. This means your client is calling a different set of jars in eclipse. In general if eclipse and the console are giving different results it's almost always a classpath issue.

Check the 'Java Build Path' section of your project and all projects that are related to make sure everything matches up. Keep an eye out for the jar that has 'com.masatosan.remote.server.MyRemoteServer', I believe that is the one that is having the version issue. The jar that eclipse is pointing to might be different and/or stale. Here are some places to check in the build path section of your project

1)Make sure you are using the 'projects' tab correctly.

2)Check to make sure all the jars on the 'libraries' tab are the same as when compiled on the console

3)Make sure that that none of the jars that should be included by the 'projects' tab are in the 'libraries' tab. I have seen many people tare their hair out because eclipse was using jars that were only updated if they compiled from the command line.

4)Play around with the 'Order and Export' tab. If something is above something else it gets loaded first. The version that is loaded first is the version that is used. If both your command line app and eclipse project have the same jars but include them in a different order you can have these issues.

5)Some jars can be installed directly in to the JRE by including them in the JREs directory. This is HIGHLY unlikely but you can check the JRE eclipse is using and make sure it's the same one as the console.

Good luck, classpath problems are very tricky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文