我有这个尝试与开放式办公室连接的测试类。
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)
现在我明白为什么会出现这个异常,但我没有得到如何解决这个问题的解决方案。