Java RMI 新手无法使用基本的 hello world 应用程序 [英] Java RMI newbie having trouble getting a basic hello world app to work

查看:63
本文介绍了Java RMI 新手无法使用基本的 hello world 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的实现类:

I wrote a simple implementation class:

public interface MyRemote extends Remote {
    public String sayHello() throws RemoteException;
}

然后是远程服务器类

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{

    protected MyRemoteImpl() throws RemoteException {

    }

    public String sayHello() throws RemoteException {
        return "Server says 'Hello World!'";
    }

    public static void main(String args[]) {
        try {
            MyRemote service = new MyRemoteImpl();
            Naming.rebind("RemoteHello", service);
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

现在我读到 rmiregistry 需要在原始类(及其存根)所在的相同路径中启动,所以我只是使用 intllij IDEA 进行编译并自动生成存根类,然后将所有类复制到我的 java 路径中有 rmiregistry.exe 文件,然后运行 ​​rmiregistry(我知道这一定是一个非常愚蠢的方法,但我只是想让它作为测试工作).每当我尝试编译服务器文件时,我仍然会收到本文底部列出的错误.

Now I read the rmiregistry needs to be started in the same path where the original classes are (and their stubs) so I just compiled using intllij IDEA and auto generated the Stub classes, then copied all the classes to my java path that has the rmiregistry.exe file and then ran the rmiregistry (I know this must be a pretty dumb method but I was just trying to get it to work as a test). I still get an error as listed at the bottom of this post whenever I try and compile the server file.

我正在工作的那本书说要使用 java 的 rmic 命令来生成存根和骨架,但我找不到这个文件(也许它来自旧版本的 JAVA?)所以我只是使用 Intellij IDEA 来生成代替存根文件...但没有生成骨架文件.

The book I am working from says to use java's rmic command to generate the stubs and skeletons but I can't find this file (maybe it's from an older version of JAVA?) so I'm just using Intellij IDEA to generate the stub file instead...but no skeleton file is generated.

我确定有一些关于 RMI 的事情我不明白,但我在网上查了一些教程,但无法弄清楚它是什么,我想也许 stackoverflow 上的某个人可以指出我正确的方向.任何帮助将不胜感激.

I'm sure there are several things about RMI I don't understand but I looked up some tutorials online and can't figure out what it is, thought maybe someone on stackoverflow could point me in the right direction. Any help would be much appreciated.

java.rmi.ServerError: 服务器线程出错;嵌套例外是: java.lang.UnsupportedClassVersionError: MyRemote :不支持的 Major.minor 版本 51.0 在sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source) atsun.rmi.server.UnicastServerRef.dispatch(Unknown Source) atsun.rmi.transport.Transport$1.run(Unknown Source) atjava.security.AccessController.doPrivileged(Native Method) 在sun.rmi.transport.Transport.serviceCall(Unknown Source) atsun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) atsun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(未知来源)在sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(未知来源)在java.util.concurrent.ThreadPoolExecutor$Worker.runTask(来源不明)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(未知来源)在 java.lang.Thread.run(Unknown Source) atsun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)在sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)在 sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377) 在sun.rmi.registry.RegistryImpl_Stub.rebind(未知来源)在java.rmi.Naming.rebind(Naming.java:177) 在MyRemoteImpl.main(MyRemoteImpl.java:27) 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:601) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

java.rmi.ServerError: Error occurred in server thread; nested exception is: java.lang.UnsupportedClassVersionError: MyRemote : Unsupported major.minor version 51.0 at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source) at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Unknown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) at java.rmi.Naming.rebind(Naming.java:177) at MyRemoteImpl.main(MyRemoteImpl.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

推荐答案

该错误基本上意味着您编译的 Java 版本高于运行它们的 Java 版本.确保您正在运行相同的 JRE 版本,或者确保您根据要运行它们的版本编译它们(使用 javac-target 选项)

The error basically means you have compiled the classes in a higher version of Java than you are running them on. Make sure you are running the same JRE version or make sure you compile them against the version you want to run them on (using -target option of javac)

阅读这篇文章了解更多详情.

这篇关于Java RMI 新手无法使用基本的 hello world 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆