如何使java ServiceLoader在NetBeans 6.9模块应用程序中工作 [英] How to make the java ServiceLoader work in a NetBeans 6.9 module application

查看:135
本文介绍了如何使java ServiceLoader在NetBeans 6.9模块应用程序中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NetBeans模块应用程序中使用java ServiceLoader时遇到了麻烦。这是我正在尝试做的(它在Eclipse中的普通java应用程序中工作):

I have troubles using the java ServiceLoader in a NetBeans module application. Here is what I'm trying to do (and it works in a normal java application in Eclipse):

我有一个interface.jar,它声明了接口。我有implements.jar,它有几个这个接口的实现,都在spi / META-INF / services / my.package.name.MyInteface文件中指定(这个文件位于implementation.jar中)。

I have an interface.jar, which declares the interface. And I have implementations.jar, which has several implementations of this interface, all specified in the spi/META-INF/services/my.package.name.MyInteface file (this file is in the implemenations.jar).

我还有一个类ImplementationHandler(在另一个handler.jar中),它有以下方法来加载所有实现:

I also have a class ImplementationHandler (in yet another handler.jar), which has the following method to load all implementations:

private static List<MyInterface<?>> loadAllImplementations() {
    List<MyInterface<?>> foundImplementations = Lists.newArrayList();
    try {
        for (MyInterface implementation : ServiceLoader.load(MyInterface.class)) {
            foundImplementations.add(implementation);
        }
    } catch (ServiceConfigurationError error) {
        system.out.println("Exception happened");
    }
    return foundImplementations;
}

此代码返回Eclipse正常应用程序中的所有实现(foundImplementations.size() > 0)。

This code returns all implementations in Eclipse normal application (the foundImplementations.size() > 0).

但是在NetBeans下,它找不到任何东西(foundImplementations.size()== 0)。

However under NetBeans, it can't find anything (foundImplementations.size() == 0).

更多详细信息:

我有NetBeans模块应用程序的源代码(开源,不是我编写的),我需要通过使用一些MyInterface实现来扩展它。 interface.jar,implements.jar和handler.jar是在Eclipse中创建的,它们是另一个应用程序的一部分。

More details:
I have the source of a NetBeans module application (open source, not written by me), which I need to extend by using some of MyInterface implementations. The interface.jar, implementations.jar and the handler.jar are created in Eclipse and they are part of another application.

在NetBeans中,我打开了需要使用新功能的模块,并将所有3个jar作为外部库添加(NetBeans将它们复制到其ext文件夹中,我不想要,但我不能做任何事情 - 我希望他们在另一个myext文件夹中,但这是另一个故事)。然后我重新构建了所有内容并尝试使用我的一个实现,但是没有找到...获得实现的代码在ImplementationHandler类中,看起来像:

In the NetBeans, I opened the module which needs to use the new impplementations and I added all my 3 jars as external libraries (NetBeans copied them into its ext folder, which I don't want but I can't do anything about - I want them in another myext folder, but that's another story). Then I rebuilt everything and tried to use one of my implementations, but it was not found... The code that gets an implementation is in the ImplementationHandler class and looks like:

public static final <T> MyInteface<T> getByName(String name) {
    for (MyInteface implementation : loadAllImplementations()) {
        if (implementation.getName().equals(name)) {
            return implementation;
        }
    }

    throw new IllegalArgumentException("Unable to find MyInterface class for: " + name);
}

我得到了异常无法找到myInteface类:myImplementationName.. 。

I got my exception "Unable to find MyInteface class for: myImplementationName"...

我对NetBeans的了解非常有限,我想知道还有什么需要做的才能让它工作吗?

My knowledge about NetBeans is very limited and I was wondering is there something more that I need to do in order to get this working?

推荐答案

我放弃了,用 JSPF 。这是开箱即用的,我不需要知道第三方程序的内部结构,编写为NetBeans模块,以及它如何影响类加载器的类路径。

I gave up, and replaced ClassLoader with JSPF. This works out of the box and I don't need to know about the internals of a third party program, written as NetBeans module and how this affects the classpath given to the class loaders.

这篇关于如何使java ServiceLoader在NetBeans 6.9模块应用程序中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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