OpenOffice学习笔记一连接OppenOffice

  其实网上已经有不少关于.Openoffice使用的文章的.我这里只不过是画蛇添足多此一举.但是.因为长期不添足.现在说都不会话了.还是写点吧.希望对你有帮助.我最讨厌废话连篇了.所以不说废话了.还有我的表达能力有限.希望大家能凑合看.不要有不清楚的地方.可以留言讨论.也可以给我发邮件..(本人邮箱垃圾邮件堆满了,不一定能看到.)现在开始讲了.还有,没有用中文标点.这属于个人习惯吧.算了废话又来了开始讲了哦.

首先需要安装Openoffice主程序.
去Openoffice主站下载.
然后安装Openofficejdk.

从jdk的example文件夹中找到了学习步骤...就按照他的走吧
下载是摘自他的源码
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the BSD license.
 *  
 *  Copyright 2000, 2010 Oracle and/or its affiliates.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *     
 *************************************************************************/

public class FirstUnoContact {

    public static void main(String[] args) {
        try {
            // get the remote office component context
            com.sun.star.uno.XComponentContext xContext =
                com.sun.star.comp.helper.Bootstrap.bootstrap();

            System.out.println("Connected to a running office ...");

            com.sun.star.lang.XMultiComponentFactory xMCF =
                xContext.getServiceManager();

            String available = (xMCF != null ? "available" : "not available");
            System.out.println( "remote ServiceManager is " + available );
        }
        catch (java.lang.Exception e){
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }
}


你运行吧肯定报错..绝对报错.
因为他的默认路径是找不到的..估计环境变量中得设置..
于是参考了一个哥们的笔记
http://www.xue5.com/Office/OpenOffice/441298_4.html


下载了个bootstrapconnector.jar


代码修改成如下:

package ansj.sun.os;

import ooo.connector.BootstrapSocketConnector;

import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;

public class ConnOpenOffice {
	 public static void main(String[] args) {
	        try {
	            // 连接OpenOffice
	        	String oooExecFolder = "C:/Program Files/OpenOffice.org 3/program/" ;
	            XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExecFolder) ;

	            System.out.println("Connected to a running office ...");

	            XMultiComponentFactory xMCF =  xContext.getServiceManager();

	            String available = (xMCF != null ? "available" : "not available");
	            System.out.println( "remote ServiceManager is " + available );
	        }
	        catch (java.lang.Exception e){
	            e.printStackTrace();
	        }
	        finally {
	            System.exit(0);
	        }
	    }
}


运行..发生错误如下...
com.sun.star.comp.helper.BootstrapException
	at ooo.connector.BootstrapConnector.connect(BootstrapConnector.java:129)
	at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:68)
	at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:45)
	at ooo.connector.BootstrapSocketConnector.bootstrap(BootstrapSocketConnector.java:82)
	at ansj.sun.os.ConnOpenOffice.main(ConnOpenOffice.java:13)


于是你打开任务管理器看看是否有
office.bin 和 office.exe 这两个进程,如果没有那就是因为Openoffice的服务没有启动.
你需要cmd到Openoffice主程序的安装目录下
cd C:\Program Files\OpenOffice.org 3\program
运行如下命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

如果没有保存哦.你再看任务管理器的进程是不是多出了office.bin 和 office.exe

现在重新运行程序

Connected to a running office ...
remote ServiceManager is available


出现如下..呵呵成功了...

你可能感兴趣的:(oracle,socket,OS,Office,sun)