1

我有这个尝试与开放式办公室连接的测试类。

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class Test {



    XMultiServiceFactory ooConnect() {

        final String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
        // create the initial component context
        XComponentContext rComponentContext = null;
        try {
            rComponentContext = Bootstrap
                    .defaultBootstrap_InitialComponentContext();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        // retrieve the servicemanager from the context
        XMultiComponentFactory rServiceManager = rComponentContext
                .getServiceManager();

        Object objectUrlResolver = null;
        try {
            objectUrlResolver = rServiceManager.createInstanceWithContext(
                    "com.sun.star.bridge.UnoUrlResolver", rComponentContext);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // Create a new url resolver
        XUnoUrlResolver xurlresolver = (XUnoUrlResolver) UnoRuntime
                .queryInterface(XUnoUrlResolver.class, objectUrlResolver);

        XMultiServiceFactory rOfficeServiceManager = null;
        try {
            // resolve the uno-url

            System.out.println("BLOCK 3");
            Object objectInitial = xurlresolver.resolve(sConnectionString);

            rOfficeServiceManager = (XMultiServiceFactory) UnoRuntime
                    .queryInterface(XMultiServiceFactory.class, objectInitial);
        } catch (Exception e) {
            e.printStackTrace();
            return rOfficeServiceManager;
        }
        return rOfficeServiceManager;
    }




    public static void main (String [] args)

    {
        Test test = new Test();

        XMultiServiceFactory serviceFactory = test.ooConnect();
        if(serviceFactory != null)
        {
            System.out.println("YEYEYEYEY");
        }
    }
}

当我尝试运行此代码时,我得到以下异常。

com.sun.star.connection.NoConnectException: Connector : couldn't connect to socket (WSANOTINITIALISED, WSAStartup() has not been called)
        at com.sun.star.bridges.jni_uno.JNI_proxy.dispatch_call(Native Method)
        at com.sun.star.bridges.jni_uno.JNI_proxy.invoke(JNI_proxy.java:171)
        at com.sun.proxy.$Proxy3.resolve(Unknown Source)
        at Test.ooConnect(Test.java:47)
        at Test.main(Test.java:66)

现在我明白为什么会出现这个异常,但我没有得到如何解决这个问题的解决方案。

4

1 回答 1

1

在 Windows 上,WinSock 库必须由进程显式初始化才能使用。这是使用该WSAStartup()功能完成的,通常在应用程序启动时。大多数其他平台在使用套接字 API 函数之前不需要显式的套接字库初始化。在 Windows 上运行时,Uno 框架应该在内部处理这个问题。如果不是,我会认为这是一个框架错误,并应向作者报告。但我发现 Uno 不太可能不考虑这一点。

WSAStartup()作为一种解决方法,您可以让您的代码在使用 Uno 之前使用 JNA 手动调用。

于 2015-06-19T17:02:10.637 回答