简体   繁体   English

java rmi简单项目classNotFoundException绑定注册表

[英]java rmi simple project classNotFoundException binding registry

Ok, I'm sure this should be pretty easy, but I'm fairly new to Java (I'm more a .NET boy :P) and after following every single recommendation I found here to no success, I think it's time to step back and ask. 好的,我确定这应该很容易,但是我对Java还是很陌生(我是一个.NET男孩:P),在遵循我在这里找到的每条建议都没有成功之后,我认为是时候退后一步,问。

I'm trying to start a simple rmi project with a client, a server and a common project where common interfaces are defined. 我正在尝试从客户端,服务器和定义了通用接口的通用项目开始一个简单的rmi项目。 I've just implemented my server code, and when I try to run it to check if everything is fine, I get struck on a java.lang.ClassNotFoundException. 我刚刚实现了服务器代码,当我尝试运行它以检查一切是否正常时,我被java.lang.ClassNotFoundException吓到了。

After following several answers on similar issues, I'm fair sure that my problem comes from rmiregistry running on a different location than my project. 在对类似问题进行了几次回答之后,我可以确定我的问题来自运行在与项目不同位置的rmiregistry。

I use following code to set registry codebase: 我使用以下代码来设置注册表代码库:

public class Utils {

public static final String CODEBASE = "java.rmi.server.codebase";

public static void setCodeBase(Class<?> c) {
    String ruta = c.getProtectionDomain().getCodeSource().getLocation().toString();

    String path = System.getProperty(CODEBASE);
    if (path != null && !path.isEmpty()) {
        ruta = path + " " + ruta;
    }

    System.setProperty(CODEBASE, ruta);
}

} }

Then, I try to start my server code with this main class: 然后,我尝试使用此主类启动服务器代码:

public class MainRegulador {

public static void main(String[] args) throws AccessException, RemoteException, NotBoundException {

    Utils.setCodeBase(IRegulador.class);
    Registry registro = null;
    Remote proxy = null;

    try {

        Regulador myReg = new Regulador();
        proxy = UnicastRemoteObject.exportObject(myReg, 36510);

        registro = LocateRegistry.getRegistry();
        registro.rebind("Distribuidor", proxy);  //this is the line where exception is thrown

        System.out.println("El Regulador está corriendo. Pulse ENTER para finalizar el proceso.");
        System.in.read();

    } catch(Exception ex) {

        System.out.println("No se ha logrado inicializar el Registrador");
        System.out.println(ex.getMessage());

    } finally {

        if (registro != null && proxy != null) {
            registro.unbind("Distribuidor");
            UnicastRemoteObject.unexportObject(proxy, true);
        }

    }
}

} }

But when I run it, always get a java.lang.ClassNotFoundException at IRegulador interface. 但是,当我运行它时,总是在IRegulador接口上获得java.lang.ClassNotFoundException。

Now the fun part: 现在有趣的部分:

  • I've printed to console java.rmi.server.codebase value, and it's pointing to bin folder of project where IRegulador interface is defined. 我已经打印到控制台java.rmi.server.codebase值,它指向定义了IRegulador接口的项目的bin文件夹。 (file:/F:/Practicas%20y%20dem%c3%a1s/Sistemas%20Distribuidos/common/bin/) (文件:/ F:/ Practicas%20y%20dem%c3%a1s / Sistemas%20Distribuidos / common / bin /)
  • Obviously, that project is also set in the classpath of server project (Regulador) 显然,该项目也在服务器项目的类路径中设置(Regulador)
  • Workspace and rmiregistry are on different disks 工作区和rmiregistry位于不同的磁盘上
  • Despite all, it doesn't seem a global classpath problem, as Utils class is on the same project as IRegulador interface, and it runs before the exception is thrown (as java.rmi.server.codebase is correctly set). 尽管如此,这似乎不是全局的类路径问题,因为Utils类与IRegulador接口位于同一项目中,并且在引发异常之前运行(因为正确设置了java.rmi.server.codebase)。

I've tried to set the classpath of rmiregistry before calling it (although it is directly discouraged on some answers), but nothing changed. 我已经尝试在调用它之前设置rmiregistry的类路径(尽管在某些答案上不建议使用它),但是没有任何改变。 I've also tried to start rmiregistry.exe from Regulador project bin folder, but also seemed to don't change anything. 我也尝试过从Regulador项目bin文件夹启动rmiregistry.exe,但似乎也没有做任何更改。

Coming from a .NET background, I've always found these classpath issues confusing, and this one is starting to consume much more time than I suspect it deserves. 从.NET背景开始,我总是发现这些类路径问题令人困惑,并且这个开始消耗的时间比我怀疑的要多得多。 I'm in desperate need of help. 我非常需要帮助。

UPDATE: I'm starting to think that the problem is within the url it's passed to the codebase from IRegulador.class. 更新:我开始认为问题出在从IRegulador.class传递给代码库的URL中。 If I paste it into windows explorer, the SO is unable to locate it, so I supose that it's being built with some structure problem that prevents the registry to reach the route: 如果我将其粘贴到Windows资源管理器中,则SO无法找到它,因此我认为它的构建存在一些结构问题,导致注册表无法到达该路由:

file:/F:/Practicas%20y%20dem%c3%a1s/Sistemas%20Distribuidos/common/bin/ 文件:/ F:/ Practicas%20y%20dem%c3%a1s / Sistemas%20Distribuidos / common / bin /

UPDATE2: I thought path route could be too complex, so I decided to simplify it and strip it from any non-straight character. UPDATE2:我认为路径路由可能太复杂了,所以我决定简化它,并将其从任何非直线字符中去除。 Now codebase value is 现在代码库的值是

file:/F:/Practicas/SD/common/bin/ 文件:/ F:/ Practicas / SD / common / bin /

However the problem persists, I don't know why rmiregistry is unable to reach that folder. 但是问题仍然存在,我不知道为什么rmiregistry无法访问该文件夹。

Then I decided to move the whole project to the same disk where rmiregistry is executed, and see if it changes anything. 然后,我决定将整个项目移至执行rmiregistry的同一磁盘上,并查看它是否有任何改变。 But nothing changed, same problem. 但是什么都没有改变,同样的问题。

Ok, finally I got it working... 好吧,终于我开始工作了...

I've just copied rmiregistry.exe into the common/bin folder and launch it directly from there (previously just had called from there). 我刚刚将rmiregistry.exe复制到common / bin文件夹中,然后直接从那里启动(以前只是从那里调用过)。

This seems to fix the problem with the routes (actually it makes the route available to the registry as it's on the same folder, probably all my codebase writting code is superflous now). 这似乎解决了路由问题(实际上,它使路由可用于注册表,因为它位于同一文件夹中,可能我现在所有的代码库编写代码都多余了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM